JSONArray和JSONObject

1.JSONArray和JSONObject是什麼

  JSONObject 就是{x:x , x:x , ...}這樣的結構json

  JSONArray 是元素是JSONOject的數組,就是[ {x:x , x:x , ...} , {x:x , x:x , ...} , ... ...]這樣的結構數組

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

  1)字符串轉JSONObejctspa

    JSONObject Jobj =JSONObject.parseObject(String str)code

  2)字符串轉JSONArrayblog

    JSONArray Jarr =JSONArray.parseArray(String str)字符串

3.獲取JSONObejct內的數據

  ·keyset   獲得key的集get

  ·getString(key) 獲得key對應的valueit

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 } }

備註:ast

用的包都是fastjsonclass

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
相關文章
相關標籤/搜索