使用JSONObject和JSONArray

前端之家收集整理的这篇文章主要介绍了使用JSONObject和JSONArray前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

JSONObject

JSONObject是一个无序的键值对集合。值可以是任意类型:Boolean,JSONArray,JSONObject,Number,String,or the JSONObject.NULL Object.

http://maps.google.com/maps/api/geocode/json?sensor=false&address=beijing 返回的json如下:

{
   "results" : [ { "address_components" : [ { "long_name" : "北京","short_name" : "北京","types" : [ "locality","political" ] },{ "long_name" : "北京市","short_name" : "北京市","types" : [ "administrative_area_level_1",{ "long_name" : "中国","short_name" : "CN","types" : [ "country","political" ] } ],"formatted_address" : "中国北京市北京市","geometry" : { "bounds" : { "northeast" : { "lat" : 41.0608158,"lng" : 117.5146251 },"southwest" : { "lat" : 39.4427581,"lng" : 115.4234115 } },"location" : { "lat" : 39.904211,"lng" : 116.407395 },"location_type" : "APPROXIMATE","viewport" : { "northeast" : { "lat" : 40.2164962,"lng" : 116.7829835 },"southwest" : { "lat" : 39.6612714,"lng" : 116.0119343 } } },"place_id" : "ChIJuSwU55ZS8DURiqkPryBWYrk","status" : "OK" }

JSONArray

JSONArray是一个有序的值序列。值可以是任意类型:Boolean,or the JSONNull object。

形如:

[ "locality","political" ]

实例:

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class JsonObjectArrayTest {

    public static void main(String[] args) {
        // 1.从字符串转换成JSONArray对象,必须首尾为[,]; 工具代码会判断是否含有'['和']'
        String types1 = "[\"locality\",\"political\"]";
        JSONArray jsonArray1 = JSONArray.fromObject(types1);
        System.out.println(jsonArray1.toString());

        // 2.从一个JSONArray转换成另一个JSONArray对象
        JSONArray jsonArray2 = JSONArray.fromObject(jsonArray1);
        System.out.println(jsonArray2.toString());

        // 3.将Collection对象,本例为ArrayList转换成JSONArray对象
        List<String> types3 = new ArrayList<String>();
        types3.add("locality");
        types3.add("political");
        JSONArray jsonArray3 = JSONArray.fromObject(types3);
        System.out.println(jsonArray3.toString());

        // 4. new 一个JSONObject对象,使用put方法添加成员,如果已经存在key,则替换value值
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("types",jsonArray3);
        System.out.println(jsonObject1.toString());

        // 5.将一个JSONObject对象转换成另一个JSONObject对象
        JSONObject jsonObject2 = JSONObject.fromObject(jsonObject1);
        System.out.println(jsonObject2);

        // 6.将一Map对象转换成JSONObject对象
        Map<String,List<String>> map = new LinkedHashMap<String,List<String>>();
        List<String> list = new ArrayList<String>();
        list.add("locality");
        list.add("political");
        map.put("types",list);
        JSONObject jsonObject3 = JSONObject.fromObject(map);
        System.out.println(jsonObject3);

        // 7.将String转换成JSONObject对象
        String str1 = "{\"types\":[\"locality\",\"political\"]}";
        JSONObject jsonObject4 = JSONObject.fromObject(str1);
        System.out.println(jsonObject4);

        // 8.累积value到这个key下,这个key下value变成JSONArray, 如果value本身就是JSONArray,直接添加到里面
        JSONObject jsonObject5 = jsonObject4.accumulate("place_id","ChIJuSwU55ZS8DURiqkPryBWYrk");
        System.out.println(jsonObject5.toString());
        JSONObject jsonObject6 = jsonObject5.accumulate("place_id","accumulate");
        System.out.println(jsonObject5.toString());
        System.out.println(jsonObject6);
        // 9.element将键值对放到JSONObject中,如果key存在,则替换原来的value; 
        // element与put的区别: 1.put返回的Object对象; element返回的是JSONObject对象
        // 2.put的key为Object对象,element的key为String
        JSONObject jsonObject7 = jsonObject6.element("types",1);
        System.out.println(jsonObject7.toString());

        // 10.使用get方法获取成员
        System.out.println(jsonObject7.getInt("types"));
        System.out.println(jsonObject7.getJSONArray("place_id")); 
        System.out.println(jsonObject7.getJSONArray("place_id").get(1));


    }

}

输出

["locality","political"]
["locality","political"]
{"types":["locality","political"]}
{"types":["locality","political"],"place_id":"ChIJuSwU55ZS8DURiqkPryBWYrk"}
{"types":["locality","place_id":["ChIJuSwU55ZS8DURiqkPryBWYrk","accumulate"]}
{"types":["locality","accumulate"]}
{"types":1,"accumulate"]}
1
["ChIJuSwU55ZS8DURiqkPryBWYrk","accumulate"]
accumulate

总结:

JSONObject添加成员的方法element()与put()的区别:

1.put返回的Object对象,element返回的是JSONObject对象

2.put的key为Object对象,element的key为String

本文介绍的方法是net.sf.json下的JSONObject和JSONArray, 而非org.json下。

wiki:

http://json-lib.sourceforge.net/apidocs/jdk15/net/sf/json/JSONObject.html
http://json-lib.sourceforge.net/apidocs/jdk15/net/sf/json/JSONArray.html

原文链接:https://www.f2er.com/json/289728.html

猜你在找的Json相关文章