JSONObject和JSONArray区别使用

发布时间 2023-03-24 00:17:36作者: cherrymint

**一:com.alibaba.fastjson**

1、JSONObject和JSONArray的区别
JSONObject obj = jsonArray.getJSONObject(0);
obj.getJSONObject("company");

[
  {
    "set": "Y",
    "CenterCode": "2021",
    "CenterName": "名字",
    "enabled": true,
    "company": [
      {
        "companyCode": "2",
        "enabled": true
      },
      {
        "companyCode": "01",
        "enabled": false
      },
      {
        "companyCode": "05",
        "enabled": true
      }
    ]
  }
]
JSONObject jsonObject = JSONObject.parseObject(data);

Map<String, Object> paramMap = new HashMap<>();
paramMap.put("hoscode",this.getHoscode());
paramMap.put("hosname",jsonObject.getString("hosname"));
paramMap.put("hostype",jsonObject.getString("hostype"));
paramMap.put("provinceCode",jsonObject.getString("provinceCode"));
paramMap.put("cityCode", jsonObject.getString("cityCode"));
paramMap.put("districtCode",jsonObject.getString("districtCode"));
paramMap.put("address",jsonObject.getString("address"));
paramMap.put("intro",jsonObject.getString("intro"));
paramMap.put("route",jsonObject.getString("route"));
//图片
paramMap.put("logoData", jsonObject.getString("logoData"));

JSONObject bookingRule = jsonObject.getJSONObject("bookingRule");
paramMap.put("bookingRule",bookingRule.toJSONString());

paramMap.put("timestamp", HttpRequestHelper.getTimestamp());
paramMap.put("sign", MD5.encrypt(this.getSignKey()));

JSONObject respone =
        HttpRequestHelper.sendRequest(paramMap,this.getApiUrl()+"/api/hosp/saveHospital");
System.out.println(respone.toJSONString());

if(null != respone && 200 == respone.getIntValue("code")) {
    return true;
} else {
    throw new HospitalException(respone.getString("message"), 201);
}

 

 

 

JSONObject表示形式 { "id": "1", "name": "张三", "sex": "男", "content": null }

JSONArray的数据表示形式(包含2个或2个以上的JSONObject)>JSONArray的数据表示形式(包含2个或2个以上的JSONObject)<br> [ { "id": "1", "name": "张三", "sex": "男", "content": null }, { "id": "1", "name": "张三", "sex": "男", "content": null } ] [ { "id": "1", "name": "张三", "sex": "男", "content": null }, { "id": "1", "name": "张三", "sex": "男", "content": null } ]</p>

String tt =  "{\"people\":[\"a\",\"b\"]}";
{
  "name": [
    "a",
    "b"
  ]
}

String test = "{"people":["a","b"]}";

2、如何从字符串String获得JSONObject对象和JSONArray对象

String test = "{"people":["a","b"]}";
JSONObject jsonObject = JSON.parseObject(test); //string转为object类型
System.out.println("===============================================================");
System.out.println("jsonObject:" + jsonObject);
JSONArray array = jsonObject.getJSONArray("name"); //输出 ["a","b"]
System.out.println("===============================================================");
System.out.println("array:" + array);
String str = JSONObject.toJSONString(array);
System.out.println("===============================================================");
System.out.println("str:" + str);

===============================================================
jsonObject:{"people":["a","b"]}
===============================================================
array:["a","b"]
===============================================================
str:["a","b"]


JSONObject jsonObject = JSON.parseObject(test); //string转为object类型
System.out.println("===============================================================");
System.out.println("jsonObject:" + jsonObject);
JSONArray array = jsonObject.getJSONArray("name"); //输出 ["a","b"]
System.out.println("===============================================================");
System.out.println("array:" + array);
String str = JSONObject.toJSONString(array);
System.out.println("===============================================================");
System.out.println("str:" + str);

===============================================================
jsonObject:{"people":["a","b"]}
===============================================================
array:["a","b"]
===============================================================
str:["a","b"]

3、如何从JSONArray中获得JSONObject对象
JSONObject obj = jsonArray.getJSONObject(0);
obj.getJSONObject("company");

JSONObject  jsonObject = jsonArray.getJSONObject(i);
[
  {
    "id": "100",
    "name": "张三",
    "title": "测试",
    "content": null
  },
  {
    "id": "101",
    "name": "李四",
    "title": "标题",
    "content": null
  }
]


String json = "[{\"id\" :\"100\", \"name\" :\"张三\", \"title\" :\"测试\", \"content\" :null },{\"id\" :\"101\", \"name\" :\"李四\", \"title\" :\"标题\", \"content\" :null }]";
JSONArray jsonArray = JSONArray.parseArray(json);
JSONObject jsonObject = jsonArray.getJSONObject(1); // 这里的jsonObject得到的数据就是第二个JSONObject
System.out.println("===============================================================");
System.out.println("jsonObject:" + jsonObject);

jsonObject:{"name":"李四","id":"101","title":"标题"}

4、获取JSON内的数据

{
  "id": "100",
  "name": "张三",
  "title": "测试",
  "content": null
}



String json = "{\"id\" :\"100\", \"name\" :\"张三\", \"title\" :\"测试\", \"content\" :null }";
 JSONObject jsonObject = JSONObject.parseObject(json);
 int ids = jsonObject.getInteger("id"); // 这里的ids得到的数据就是100.
 String names = jsonObject.getString("name"); // 这里的names得到的数据就是张三.
 System.out.println("===============================================================");
 System.out.println("ids:" + ids);
 System.out.println("names:" + names);

===============================================================
ids:100
names:张三

二:net.sf.json

转载原文链接:https://blog.csdn.net/weixin_44563573/article/details/122344628