天天看點

JSONObject用法

JSONObject

取字元串getString

轉JSONObject 對:JSON.parseObject

JSONObject jsonObjectTemplate = yunzhijiaUtils.getTemplateByCodeId(param5);
        String data = jsonObjectTemplate.getString("data");
        JSONObject dataObj =JSON.parseObject(data);
        String basicInfo =dataObj.getString("basicInfo");
        JSONObject basicInfoObj =JSON.parseObject(basicInfo);
    String formDefId = basicInfoObj.getString("formDefId");      
JSONObject jsonObject = JSON.parseObject(res);
        String data = jsonObject.getString("data");
        JSONObject jsonObjects = JSON.parseObject(data);
        JSONArray ja = jsonObjects.getJSONArray("rows");
        for (int i = 0; i < ja.size(); i++) {
            JSONObject jo = ja.getJSONObject(i);
            String op_way = jo.getString("op_way");
            String op_time = jo.getString("op_time");
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            long lt = new Long(op_time);
            Date date = new Date(lt);
            res = simpleDateFormat.format(date);
            String pwd_no = jo.getString("pwd_no");
            String user_mobile = jo.getString("user_mobile");
            System.out.println(op_way + res + pwd_no + user_mobile + "------------");
        }      

 JSONArray widgetValue = JSONArray.fromObject(widgetValueStr);

<!--json數組,字元串轉換-->
<dependency>
    <groupId>net.sf.json-lib</groupId>
    <artifactId>json-lib</artifactId>
    <version>2.4</version>
    <classifier>jdk15</classifier>
</dependency>      

import net.sf.json.JSONArray;

import net.sf.json.JSONObject;

使用:

JSONArray array = JSONArray.fromObject("JSON數組字元串");

JSONObject jsonObject = JSONObject.fromObject("JSON格式字元串");

{
        "type":2,
        "name":"222",
        "content":{
            "title":"222",
            "list":[
                {"title":"222","content":"222"},
                {"title":"222","content":"22222\n33333\n44444"}
            ]}
 }