您的位置:首页 > Web前端 > JavaScript

Json解析

2015-09-08 10:42 579 查看

1.解析键值对的json

//要解析的JSON字符串

//  {   

//   "phone" : ["12345678", "87654321"] , 

//   "name" : "tianjiefeng",

//   "age" : 100, 

//   "address" : { "country" : "china", "province" : "jiangsu" },

//   "married" : false

//   } 

  

//  方法一:用JSONObject解析Json

 Map<String , Object> m=new HashMap<String, Object>();

  try {

 JSONObject  person=new JSONObject(js);

   m.put("phone",person.getJSONArray("phone"));

   m.put("name", person.getString("name"));

   m.put("age", person.getInt("age"));

   m.put("address", person.getJSONObject("address").toString());

   m.put("married", person.getBoolean("married"));

  } catch (JSONException e) {

   // TODO Auto-generated catch block

   e.printStackTrace();

  }

  Log.d("js",m.get("phone").toString()+m.get("name")+m.get("age")+""+m.get("address").toString()+m.get("married")+"");

//  方法二:用Gson 解析Json

  Gson gson=new Gson();

  User u=gson.fromJson(js,User.class);

  Log.d("gson",u.getPhone().get(0)+u.getPhone().get(1)+u.getName()+u.getAge()+""+u.getAddress().getCountry()+u.getAddress().getProvince()+u.isMarried()+"");

 }

         

     类里面的属性名必须跟Json字段里面的Key是一模一样的;

         public class User {

           private List<String> phone;

           private String name;

           private int age;

           private Address address;

           private boolean married;

           //必须写set get方法

        }

2.非键值对json解析

需要解析的json字符串 (json数组)

[

    {

        "cellphone": "15184418815",

        "content": "content93",

        "createDate": "2015-06-18T15:49:06.18",

        "custId": "10083",

        "id": "558277f25b61e0b7654d9a56"

    },

    {

        "cellphone": "15184418815",

        "content": "content92",

        "createDate": "2015-06-18T15:49:06.157",

        "custId": "10083",

        "id": "558277f25b61e0b7654d9a55"

    }

]

解析方法:

   Gson gson = new Gson();

    JsonParser parser = new JsonParser();

       JsonArray jArray = parser.parse((String)response).getAsJsonArray();

       ArrayList<SystemMessage> lcs = new ArrayList<SystemMessage>();

       for(JsonElement obj : jArray )

       {

        SystemMessage cse = gson.fromJson( obj , SystemMessage.class);

           lcs.add(cse);

       }

SystemMessage 实体对象:

public class SystemMessage implements Serializable{

 public String  cellphone;

 public String   content;

 public int  contentType;

 public String createDate;

 public String  custId;

 public String  id;

 public int  type;

}

需要解析的json字符串(json对象)

[

    {

        "code": "123123",

        "createDate": "2015-06-08T11:00:09",

        "fileUrl": "111",

        "fileVersion": "111",

        "homeUrl": "111",

        "id": 1,

        "name": "默认站点",

        "updateDate": "2015-06-08T11:14:37"

    }

]

Gson gson = new Gson();

Type listType = new TypeToken<List<Activity>>(){}.getType(); 

List<Activity> activities = gson.fromJson((String)response, listType);

Activity实体类

public class Activity  implements Serializable{

 /**

  *

  */

 private static final long serialVersionUID = 1L;

 /**

  *

  */

   public String updateDate;

   /**

    * 活动名称

    */

   public String name;

   /**

    *

    */

   public String id;

   /**

    *

    */

   public String homeUrl;

   /**

    * 文件版本好

    */

   public String fileVersion;

   /**

    * 文件地址

    */

   public String fileUrl;

   /**

    * 创建时间

    */

   public String createDate;

   /**

    * 活动编号

    */

   public String code;

}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息