普通json数据实例:
{
"name":"Michael",
"address":
{
"city":"beijing",
"street":"Chaoyang Road",
"postcode":"100025"
}
}
使用Google API解析json
private String jsonData = "[{\"name\":\"Michael\",\"age\":20},{\"name\":\"Mike\",\"age\":21}]";
JsonUtils jsonUtils = new JsonUtils();
jsonUtils.parseJson(jsonData);
public class JsonUtils {
public void parseJson(String jsonData){
try{
//如果需要解析JSON数据,首要要生成一个JsonReader对象
JsonReader reader = new JsonReader(new StringReader(jsonData));
reader.beginArray();
while(reader.hasNext()){
reader.beginObject();
while(reader.hasNext()){
String tagName = reader.nextName();
if(tagName.equals("name")){
System.out.println("name--->" + reader.nextString());
}
else if(tagName.equals("age")){
System.out.println("age--->" + reader.nextInt());
}
}
reader.endObject();
}
reader.endArray();
}
catch(Exception e){
e.printStackTrace();
}
}
}
public class User {
private String name ;
private int age ;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
public class JsonUtils {
public void parseUserFromJson(String jsonData){
Gson gson = new Gson();
User user = gson.fromJson(jsonData, User.class);
System.out.println("name--->" + user.getName());
System.out.println("age---->" + user.getAge());
}
}
private String jsonData = "{\"name\":\"Michael\",\"age\":20}";
JsonUtils jsonUtils = new JsonUtils();
jsonUtils.parseUserFromJson(jsonData);
public class User {
private String name ;
private int age ;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
private String jsonData = "[{\"name\":\"Michael\",\"age\":20},{\"name\":\"Mike\",\"age\":21}]";
public class JsonUtils {
public void parseUserFromJson(String jsonData){
Type listType = new TypeToken<LinkedList><User>>(){}.getType();
Gson gson = new Gson();
LinkedList<User> users = gson.fromJson(jsonData, listType);
for (Iterator iterator = users.iterator(); iterator.hasNext();) {
User user = (User) iterator.next();
System.out.println("name--->" + user.getName());
System.out.println("age---->" + user.getAge());
}
}
}
JsonUtils jsonUtils = new JsonUtils();
jsonUtils.parseUserFromJson(jsonData);
private void startUrlCheck(String username,String password)
{
HttpClient client = new DefaultHttpClient();
StringBuilder builder = new StringBuilder();
HttpGet myget = new HttpGet("http://10.0.2.2/Android/index.php");
try {
HttpResponse response = client.execute(myget);
BufferedReader reader = new BufferedReader(new InputStreamReader(
response.getEntity().getContent()));
for (String s = reader.readLine(); s != null; s = reader.readLine()) {
builder.append(s);
}
JSONObject jsonObject = new JSONObject(builder.toString());
String re_username = jsonObject.getString("username");
String re_password = jsonObject.getString("password");
int re_user_id = jsonObject.getInt("user_id");
setTitle("用户id_"+re_user_id);
Log.v("url response", "true="+re_username);
Log.v("url response", "true="+re_password);
} catch (Exception e) {
Log.v("url response", "false");
e.printStackTrace();
}
}
--转自
该贴由koei123转至本版2015-7-14 11:05:54