java中JSONObject(Json字符串)和JSONArray区别、用法

JSONObject和JSONArray区别(java中)、用法

一、JSONObject 和 JSONArray表现形式的区别

(1)、JSONObject的数据是最外面用 { } 括起来表示的:
		例如:{  "channelId" :  "architectural"  ,  "jsonrpc" : "2.0"  ,"  id" : 1 }
(2)、JSONArray:其实就是多个JSONObject构成的数组,用 [ { } , { } , … , { } ] 来表示的(最外面是用 [ ] 括起来的)
		例如:[ {  "keep" : "88.0" } , { "das" :50 , "method" : "Chain33.QueryTransaction" } ]

二、JSONObject 和 JSONArray在java中是如何使用的,如下:

(1)、这个JSONObject: { “channelId” : “architectural” , “jsonrpc” : “2.0” ," id" : 1 }的生成过程:
		JSONObject jsonObject=new JSONObject();
		jsonObject.put("channelId","architectural");
		jsonObject.put("jsonrpc","2.0");
		jsonObject.put("id",1);
取值过程:
		jsonObject.getString("channelId") 取出来的值为:"architectural"
		jsonObject.getString("jsonrpc") 取出来的值为:"2.0"
		jsonObject.getInteger("id") 取出来的值为:1
(2)、这个JSONArray:[ { “keep” : “88.0” } , { “das” : 50 , “method” : “Chain33.QueryTransaction” } ]的生成过程:
		JSONObject jsonObject1=new JSONObject();
		jsonObject1.put("keep","88.0");
		JSONObject jsonObject2=new JSONObject();
		jsonObject2.put("das",50);
		jsonObject2.put("method","Chain33.QueryTransaction");
		JSONArray jsonArray=new JSONArray();
		jsonArray.add(jsonObject1);
		jsonArray.add(jsonObject2);
取值过程:
		jsonArray.get(1) 取出来的为:jsonObject1;
		jsonArray.get(2) 取出来的为:jsonObject2;
		
	 jsonObject1和jsonObject2中的值可以利用JSONObject的取值方法(二、中的(1))去取值即可

三、如何将字符串转为 JSONObject对象 和 JSONArray对象

(1)、字符串String object=" { “channelId” : “architectural” , “jsonrpc” : “2.0” ," id" : 1 }"转为 JSONObject对象:
		JSONObject  jsonObject  =JSON.parseObject ( object );
取值过程:
		jsonObject.getString("channelId") 取出来的值为:"architectural"
		jsonObject.getString("jsonrpc") 取出来的值为:"2.0"
		jsonObject.getInteger("id") 取出来的值为:1
(2)、字符串String array=" [ { “keep” : “88.0” } , { “das” : 50 , “method” : “Chain33.QueryTransaction” } ] "转为JSONArray对象:
		JSONArray jsonArray = JSON.parseArray ( array ) ;
取值过程:
		JSONObject jsonObject1=jsonArray.get(1);
		JSONObject jsonObject2=jsonArray.get(2);
		jsonObject1.getString("keep") 取出来的值为:“88.0”
		jsonObject2.getInteger("das") 取出来的值为:  50
		jsonObject2.getString("method") 取出来的值为: “Chain33.QueryTransaction”

四、用具体实例说明如何创建json字符串,如何解析json字符串

(1)、创建json字符串:
	 “ {
		  	"channelId" : "architec",
		  	"data":[ { "jsonrpc" : "23.0",
						 "id" : 55,
						 "method" : "Chain33",
						 "params" : [ { "hash" : "0x608" } ]
					} ]
							 } ”
创建过程:
	先创建 [ { "hash" : "0x608" } ]
		JSONArray params=new JSONArray();
		JSONObject hash=new JSONObject();
			hash.put("hash","0x608");
			params.add(hash);
	再创建"data":[ { "jsonrpc" : "23.0" , "id" : 55 , "method" : "Chain33", "params" : [{ "hash" : "0x608" }]   } ]
		JSONArray data=new JSONArray();
		JSONObject jimp=new JSONObject();
			jimp.put("jsonrpc","23.0");
			jimp.put("id",55);
			jimp.put("method","Chain33");
			jimp.put("params",params);
		 data.add(jimp);
	最后生成整个json字符串
		JSONObject responsData=new JSONObject();
			responsData.put("channelId","architec");
			responsData.put("data",data);
		String jsonString=responsData.toString();

其中jsonString就是所要的json字符串

(2)、解析json字符串:String jsonString=“ { “channelId” : “architec”,“data”:[ { “jsonrpc” : “23.0”,“id” : 55,“method” : “Chain33”,“params” : [{ “hash” : “0x608” }] } ] } ”,取出 id 这个key对应的值 55
取值过程:
		JSONObject responsData= JSON.parseObject(jsonString);
    	JSONArray data=responsData.getJSONArray("data");
    	for (Object o : data) {
        	JSONObject idObject = JSONObject.parseObject(JSONObject.toJSONString(o));
        	Integer id=idObject.getInteger("id");
    	}

其中id就是取出的55

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JSON字符串JSONArray可以通过JavaJSONObjectJSONArray类来实现。首先,我们需要将JSON字符串转换为JSONObject对象,然后使用getJSONArray方法获取JSONArray对象。 例如,假设我们有一个JSON字符串如下: { "students": [ {"name": "Tom", "age": 18}, {"name": "Jerry", "age": 20}, {"name": "Mary", "age": 19} ] } 我们可以使用以下代码将其转换为JSONArray对象: String jsonString = "{\"students\":[{\"name\":\"Tom\",\"age\":18},{\"name\":\"Jerry\",\"age\":20},{\"name\":\"Mary\",\"age\":19}]}"; JSONObject jsonObject = new JSONObject(jsonString); JSONArray jsonArray = jsonObject.getJSONArray("students"); 上述代码jsonString是我们需要转换的JSON字符串,使用JSONObject将其转换为JSONObject对象,然后调用getJSONArray方法获取其的“students”数组,并将其转换为JSONArray对象。 在JSONArray对象,可以使用length方法获取JSONArray的长度,使用get方法根据索引获取其的元素。例如,我们可以使用以下代码遍历上述的JSONArray: for (int i = 0; i < jsonArray.length(); i++) { JSONObject student = jsonArray.getJSONObject(i); String name = student.getString("name"); int age = student.getInt("age"); System.out.println("Name: " + name + ", Age: " + age); } 上述代码,我们使用getJSONObject方法根据索引获取JSONArrayJSONObject对象,并使用getString和getInt方法获取其的数据。然后可以将这些数据用于后续的处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值