利用JsonObject和JsonArray解析Josn
2016-06-02 17:37
465 查看
一直没用过。JsonObject和JsonArray.今天写个Demo。代码里面就分析了。就不在这浪费时间了。
请看原理代码(明白什么是 JsonObject 和 JosnArray ) :
转:[]http://www.2cto.com/kf/201502/377165.html)]
请看测试代码:
请看原理代码(明白什么是 JsonObject 和 JosnArray ) :
转:[]http://www.2cto.com/kf/201502/377165.html)]
1、代码 public class MainActivity extends Activity { TextView tv_json; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv_json = (TextView) findViewById(R.id.tv_json); JSONArray ja = new JSONArray();//将JsonObject对象放入 JSONArray ja2 = new JSONArray();//将JsonObject对象放入 JSONObject jsonInfo = new JSONObject();//存放jsonArray数据 实现key-value try { jsonObject.put("name", "tom"); jsonObject.put("password", "123"); jsonObject.put("sex", "man"); jsonObject.put("age", "20"); jsonObject2.put("name2", "tom"); jsonObject2.put("password2", "123"); jsonObject2.put("sex2", "man"); jsonObject2.put("age2", "20"); ja.put(jsonObject); ja2.put(jsonObject2); jsonInfo.put("first", ja); jsonInfo.put("second", ja2); System.out.println("jsoninfo==="+jsonInfo.toString()); tv_json.setText("sum===" + jsonInfo.toString()); JSONObject getJson = new JSONObject(jsonInfo.toString()); JSONArray jArray=(JSONArray) getJson.get("first"); for(int i=0;i JSONObject o=(JSONObject) jArray.get(i); System.out.println("o.name==="+o.getString("name")); } } catch (JSONException e) { e.printStackTrace(); } } } 2、输出结果: jsoninfo==={"second":[{"age2":"20","sex2":"man","name2":"tom","password2":"123"}],"first":[{"password":"123","sex":"man","age":"20","name":"tom"}]} o.name===tom 2.2、json数据 ,无序 { "second": [ { "age2": "20", "sex2": "man", "name2": "tom", "password2": "123" } ], "first": [ { "password": "123", "sex": "man", "age": "20", "name": "tom" } ] }
请看测试代码:
我的数据: "method": "[{\"img\":\"http://f2.mob.com/null/2015/08/19/1439946000698.jpg\",\"step\":\"1.将鸡整理干净后放入温水锅,下姜、葱段、盐,加盖用大火烧沸后改小火煮十五至二十分钟(视鸡的品种和老嫩而定),把锅端离火炉,让鸡在汤里浸泡约十分钟。\"},{\"img\":\"http://f2.mob.com/null/2015/08/19/1439946000901.jpg\",\"step\":\"2.将鲜花椒、留下的一棵葱、微量盐、半咖啡匙香油放菜板上剁成细末。\"},{\"img\":\"http://f2.mob.com/null/2015/08/19/1439946001241.jpg\",\"step\":\"3.盛小碗里,加入酱油、味精、香油、花椒油拌成味料。\"},{\"step\":\"4.将鸡取出晾凉,斩成条后整齐地码在盘子里。浇上味料即可上桌。\"}] ========================================= 注意他是一个数组里面的串。首先得到里面数据的长度,也就是遍历里面 JSON 键值对的次数 这个数据我们通过 intent.putString 方式拿到。然后我们就可以通过 1. JSONArray jsonArray = new JSONArray(zuofa); 得到长度。就有了 下一步 for(int i=0 ; i<jsonArray.length(); i++){ 里面需要一个JsonObject 来获取对应位置 i位置 Json键值对 JSONObject jsonObject = newJSONObject(jsonArray.getString(i)); 获取到JsonObject 对象。我们就需要根据对象的键,拿到对应的值。 String step = jsonObject.getString("step"); 有的Json需要 一个 图片,有的没有,那么 我就通过判断 jsonObject来解决的。 如果长度超过了 仅仅有描述的。那么就有图片。然后就拿到图片 添加到集合中 然后也要记得吧 描述添加到 集合中 完整代码: 4000 try { JSONArray jsonArray = new JSONArray(zuofa); for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObject = new JSONObject(jsonArray.getString(i)); String step = jsonObject.getString("step"); System.out.println("jsonObject" + jsonObject + "\n"); if (jsonObject.length() > 1) { String stepIamge = jsonObject.getString("img"); System.out.println("stepImage:" + stepIamge + "\n"); zuofa_image.add(stepIamge); // 路径添加 } System.out.println(step); zuofa_desc.add(step); // 内容添加 }
相关文章推荐
- 使用 Python 处理 JSON 格式的数据
- XML 与 JSON 优劣对比
- VBA将excel数据表生成JSON文件
- newtonsoft.json解析天气数据出错解决方法
- vbs 解析json jsonp的方法
- Ruby和Ruby on Rails中解析JSON格式数据的实例教程
- Extjs4如何处理后台json数据中日期和时间
- C#实现将类的内容写成JSON格式字符串的方法
- JQuery ajax返回JSON时的处理方式 (三种方式)
- jquery JSON的解析方式示例介绍
- c#版json数据解析示例分享
- ASP JSON类文件的使用方法
- C# JSON格式化转换辅助类 ConvertJson
- C#实现对Json字符串处理实例
- C#实现Json转Unicode的方法
- php解析json数据实例
- PHP中JSON的应用技巧
- PHP6 先修班 JSON实例代码
- 关于JSON以及JSON在PHP中的应用技巧
- .Net中的json操作类用法分析