组装Json

本文介绍如何使用Java生成复杂的JSON对象,并展示了如何从JSON对象中提取数据。通过四个不同类型的示例,介绍了创建JSON对象和数组的方法,同时给出了具体的解析过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、组装成如下形式

[java]  view plain  copy
  1. {  
  2.     "a""a_value",  
  3.     "b""b_value",  
  4.     "c": [  
  5.         "c1_value",  
  6.         "c2_value"  
  7.     ],  
  8.     "d": {  
  9.         "d1""d1_value",  
  10.         "d2""d2_value"  
  11.     },  
  12.     "e": [  
  13.         {  
  14.             "e1""e1_value",  
  15.             "e2""e2_value"  
  16.         },  
  17.         {  
  18.             "e3""e3_value",  
  19.             "e4""e4_value"  
  20.         }  
  21.     ]  
  22. }  
点击button生成Json,直接上代码:

[java]  view plain  copy
  1. private void toJson() {  
  2.     JSONObject jsonObject = new JSONObject();  
  3.     try {  
  4.         //形式1  
  5.         jsonObject.put("a""a_value");  
  6.         jsonObject.put("b""b_value");  
  7.         //形式2  
  8.         JSONArray json_c = new JSONArray();  
  9.         json_c.put("c1_value");  
  10.         json_c.put("c2_value");  
  11.         jsonObject.put("c", json_c);  
  12.         //形式3  
  13.         JSONObject json_d = new JSONObject();  
  14.         json_d.put("d1""d1_value");  
  15.         json_d.put("d2""d2_value");  
  16.         jsonObject.put("d", json_d);  
  17.         //形式4  
  18.         JSONArray json_e = new JSONArray();  
  19.         JSONObject childer_1 = new JSONObject();  
  20.         childer_1.put("e1""e1_value");  
  21.         childer_1.put("e2""e2_value");  
  22.         json_e.put(childer_1);  
  23.         JSONObject childer_2 = new JSONObject();  
  24.         childer_2.put("e3""e3_value");  
  25.         childer_2.put("e4""e4_value");  
  26.         json_e.put(childer_2);  
  27.         jsonObject.put("e", json_e);  
  28.     } catch (JSONException e) {  
  29.         e.printStackTrace();  
  30.     }  
  31.     tv.setText(jsonObject.toString());  
  32. }  


2、将如上json解析出来

[java]  view plain  copy
  1. private void jsonToString(){  
  2.     StringBuffer sb = new StringBuffer(256);  
  3.     try {  
  4.         //得到类型1  
  5.         String a = jsonObject.getString("a");  
  6.         String b = jsonObject.getString("b");  
  7.         sb.append(a+"\n"+b+"\n");  
  8.         //类型2  
  9.         JSONArray cArray = jsonObject.getJSONArray("c");  
  10.         for (int i = 0; i < cArray.length(); i++) {//将数组中的内容分别显示出来  
  11.             sb.append(cArray.getString(i)+"\n");  
  12.         }  
  13.         //类型3  
  14.         JSONObject d = jsonObject.optJSONObject("d");  
  15.         String d1 = d.getString("d1");  
  16.         String d2 = d.getString("d2");  
  17.         sb.append(d1+"\n"+d2+"\n");  
  18.         //类型4  
  19.         JSONArray eArray = jsonObject.getJSONArray("e");//得到对应的数组  
  20.         JSONObject chileren_1 = eArray.optJSONObject(0);//得到数组中第一段object数据  
  21.         String e1 = chileren_1.getString("e1");  
  22.         String e2 = chileren_1.getString("e2");  
  23.         JSONObject chileren_2 = eArray.optJSONObject(1);//得到数组中第二段object数据  
  24.         String e3 = chileren_2.getString("e3");  
  25.         String e4 = chileren_2.getString("e4");  
  26.         sb.append(e1+"\n"+e2+"\n"+e3+"\n"+e4+"\n");  
  27.   
  28.         tv.setText(sb.toString());  
  29.   
  30.     } catch (JSONException e) {  
  31.         e.printStackTrace();  
  32.     }  
  33. }  



转载自:http://blog.youkuaiyun.com/jiangtea/article/details/53259955

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值