JSONArray和JSONObject

1.JSONArray和JSONObject是什么

  JSONObject 就是{x:x , x:x , ...}这样的结构

  JSONArray 是元素是JSONOject的数组,就是[ {x:x , x:x , ...} , {x:x , x:x , ...} , ... ...]这样的结构

2.字符串转JSONArray和JSONObejct的互相转化

  1)字符串转JSONObejct

    JSONObject Jobj =JSONObject.parseObject(String str)

  2)字符串转JSONArray

    JSONArray Jarr =JSONArray.parseArray(String str)

3.获取JSONObejct内的数据

  ·keyset   得到key的集

  ·getString(key) 得到key对应的value

4.遍历

       //假设  str ==  {"total":8109 , "rows":"[{ ...},{ ...},{ ...}]"}
     JSONObject jsonObject = JSONObject.parseObject(Str); JSONArray jsonArray = (JSONArray) jsonObject.get(Key); JSONObject tempJSon; for (Object obj : jsonArray) { tempJSon = (JSONObject) obj; for (String str : tempJSon.keySet()) { //key == str //value == tempJSon.getString(str) pass } }

备注:

用的包都是fastjson

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
posted @ 2019-03-15 15:43  木叶小寒江  阅读(150)  评论(0编辑  收藏  举报