利用JsonObject和JsonArray解析Josn

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

一直没用过。JsonObject和JsonArray.今天写个Demo。代码里面就分析了。就不在这浪费时间了。

请看原理代码(明白什么是 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来解决的。
       如果长度超过了 仅仅有描述的。那么就有图片。然后就拿到图片
       添加到集合中
       然后也要记得吧 描述添加到 集合中

完整代码:
   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);  // 内容添加
            }
原文链接:https://www.f2er.com/json/289333.html

猜你在找的Json相关文章