关闭

json解析

标签: jsonstringuserclassimportandroid
457人阅读 评论(0) 收藏 举报
分类:

 private String json =
   "[   " +
    "{     " +
     "\"id\": 912345678901," +
     "\"text\": \"How do I read JSON on Android?\"," +
     "\"geo\": null," +
     "\"user\": " +
     "{" +
      " \"name\": \"android_newb\"," +
      "\"followers_count\": 41" +
     "}" +
    "},"  +
    "{" +
     "\"id\": 912345678902," +
     "\"text\": \"@android_newb just use android.util.JsonReader!\"," +
     "\"geo\": [50.454722, -104.606667]," +
     "\"user\": " +
     "{" +
      "\"name\": \"jesse\"," +
      "\"followers_count\": 2" +
     "}" +
    "}" +
   "]";

第一种解析:

package com.zhg.json1;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class JsonParser {
 public ArrayList<Demo> parse2List(String json) {
  ArrayList<Demo> demos = new ArrayList<Demo>();
  try {
   JSONArray array = new JSONArray(json);
   for (int i = 0; i < array.length(); i++) {
    Demo demo = new Demo();
    JSONObject obj = array.getJSONObject(i);
    demo.setId(obj.getLong("id"));
    demo.setText(obj.getString("text"));
    if (!obj.isNull("geo")) {
     double[] geo = new double[2];
     JSONArray geoArray = obj.getJSONArray("geo");
     geo[0] = geoArray.getDouble(0);
     geo[1] = geoArray.getDouble(1);
     demo.setGeo(geo);
    }
    JSONObject userObj = obj.getJSONObject("user");
    User user = new User();
    user.setName(userObj.getString("name"));
    user.setFollower_count(userObj.getInt("followers_count"));
    demo.setUser(user);

    demos.add(demo);
   }
  } catch (JSONException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return demos;
 }

 public String parse2Json(ArrayList<Demo> demos) {
  String json = null;
  try {
   JSONArray array = new JSONArray();
   for (Demo demo : demos) {
    JSONObject obj = new JSONObject();
    obj.put("id", demo.getId());
    obj.put("text", demo.getText());
    if (demo.getGeo() == null) {
     obj.put("geo", JSONObject.NULL);
    } else {
     JSONArray geoArray = new JSONArray();
     geoArray.put(demo.getGeo()[0]);
     geoArray.put(demo.getGeo()[1]);
     obj.put("geo", geoArray);
    }
    JSONObject userObj = new JSONObject();
    userObj.put("name", demo.getUser().getName());
    userObj.put("followers_count", demo.getUser().getFollower_count());
    obj.put("user", userObj);
    array.put(obj);
   }
   json = array.toString();
  } catch (JSONException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return json;
 }
}

 

第二种解析:

package com.zhg.json2;

import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;

import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;

public class JsonParser {
 public ArrayList<Demo> parse2List(String json){
  ArrayList<Demo> demos = new ArrayList<Demo>();
  try {
   StringReader sr = new StringReader(json);
   JsonReader reader = new JsonReader(sr);
   reader.beginArray();
   while(reader.hasNext()){
    Demo demo = new Demo();
    reader.beginObject();
    while(reader.hasNext()){
     String name = reader.nextName();
     if(name.equals("id")){
      demo.setId(reader.nextLong());
     }else if(name.equals("text")){
      demo.setText(reader.nextString());
     }else if(name.equals("geo")){
      double[] geo = null;
      if(reader.peek()==JsonToken.NULL){
       reader.nextNull();
      }else{
       geo = new double[2];
       int i = 0;
       reader.beginArray();
       while(reader.hasNext()){
        geo[i++] = reader.nextDouble();
       }
       reader.endArray();
      }
      demo.setGeo(geo);
     }else if(name.equals("user")){
      User user = new User();
      reader.beginObject();
      while(reader.hasNext()){
       String uName = reader.nextName();
       if(uName.equals("name")){
        user.setName(reader.nextString());
       }else if(uName.equals("followers_count")){
        user.setFollower_count(reader.nextInt());
       }
      }
      reader.endObject();
      demo.setUser(user);
     }
    }
    reader.endObject();
    demos.add(demo);
   }
   reader.endArray();
   sr.close();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return demos;
 }
 
 public String parse2Json(ArrayList<Demo> demos){
  String json = null;
  try {
   StringWriter sw = new StringWriter();
   JsonWriter writer = new JsonWriter(sw);
   writer.beginArray();
   for(Demo demo : demos){
    writer.beginObject();
    writer.name("id").value(demo.getId());
    writer.name("text").value(demo.getText());
    if(demo.getGeo()==null){
     writer.name("geo").nullValue();
    }else{
     writer.name("geo");
     writer.beginArray();
     writer.value(demo.getGeo()[0]);
     writer.value(demo.getGeo()[1]);
     writer.endArray();
    }
    writer.name("user");
    writer.beginObject();
    writer.name("name").value(demo.getUser().getName());
    writer.name("followers_count").value(demo.getUser().getFollower_count());
    writer.endObject();
    writer.endObject();
   }
   writer.endArray();
   writer.close();
   json = sw.toString();
   sw.close();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return json;
 }
}

 

第三种解析:

package com.zhg.utils;

import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;

import com.google.gson.stream.JsonReader;
import com.zhg.entity.Student;

public class JsonParser {
 public ArrayList<Student> parseToList(Reader in){
  ArrayList<Student> stus = new ArrayList<Student>();
  try {
   JsonReader reader = new JsonReader(in);
   reader.beginArray();
   while(reader.hasNext()){
    Student stu = new Student();
    reader.beginObject();
    while(reader.hasNext()){
     String name = reader.nextName();
     if(name.equals("id")){
      stu.setId(reader.nextInt());
     }else if (name.equals("name")){
      stu.setName(reader.nextString());
     }else if(name.equals("sex")){
      stu.setSex(reader.nextString());
     }else if(name.equals("age")){
      stu.setAge(reader.nextInt());
     }
    }
    reader.endObject();
    stus.add(stu);
   }
   reader.endArray();
   reader.close();
   in.close();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return stus;
 }
}

 

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:155493次
    • 积分:2643
    • 等级:
    • 排名:第13755名
    • 原创:106篇
    • 转载:47篇
    • 译文:1篇
    • 评论:5条
    文章分类
    最新评论