Java com.alibaba.fastjson常用方法

JSONObject转JSONString:

JSON.toJSONString(jsonObject);

JSONString转JSONObject:

JSONObject.parseObject(jsonString);

从JSONObject中获得JSONArray:

JSONArray jsonArray=jsonObject.getJSONArray(key);

JSONArray转JSONObject再转JSONString:

map.put(key,jsonArray);//通过map转成JSONObject

JSON.toJSONString(map)//再转成JSONString

JSONObject转Record:

record.set(jsonObject);

Record转JSONObject:

JSONObject jsonObject=JSONObject.parseObject(JSON.toJSONString(record.getColumns()));

遍历JSONArray:

for(int i=0;i<jsonArray.size();i++){
    JSONObject jsonObject=jsonArray.getJSONObject(i);//通过下标取元素
    String str=jsonObject.getString(key);//通过key取JSON对象里的value
}

猜你喜欢

转载自blog.csdn.net/Cary_1029/article/details/84720936