1. 程式人生 > 程式設計 >Java使用fastjson對String、JSONObject、JSONArray相互轉換

Java使用fastjson對String、JSONObject、JSONArray相互轉換

目錄
  • faston對String、JSONObject、JSONArray相互轉換
  • com.alibaba.fastjson.JSONObject、JSONArray與String之間的轉換demo

fastjson對String、JSONObject、JSONArray相互轉換

fastjson是阿里巴巴的開源JSON解析庫,它可以解析JSON格式的字串,支援將 Bean序列化為JSON字串,也可以從JSON字串反序列化到JavaBean

下面主要是本人在工作中經常用到的關於String、JSONObject、JSONArray的相互裝換

String——>>>JSONArray

String st = "[{name:Tim,age:25,sex:male},{name:Tom,age:28,{name:Lily,age:15,sex:female}]";
JSONArray tableData = JSONArray.parseArray(st);

JSONArray——>>>JSONObject

JSONObject rowData = new JSONObject();
for(int i;i<tableData.length();i++){
    rowData = tableData.getJSONObject[i];
}

String——>>>JSONObject

String st = "{name:Tim,sex:male}";
JSONObject rowData = JSONObject.parseObject(st);

JSONObject——>>>JSONArray

LYQTSgcVn
JSONObject rowData = {info:
                            [
                                {
                                    name:Tim,sex:male
                                },{
                                    name:Tom,{
                                    name:Lily,sex:female
                                }
                            ]
                        };
JSONArry tableData = rowData.get("info");

com.alibaba.fastjson.JSONObject、JSONArray與String之間的轉換demo

話不多說,直接上程式碼

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.util.Map;
/**
 * Created by LH on 2019/2/21 14:08
 */
public class JsonDemo {
    public static void main(String[] args) {
        //1.json字串轉換為物件
        String jsonString="{'name':'42313123','id':'2345','age':12}";
        JSONObject jsonObject = JSONObject.parseObject(jsonString);
        String id = jsonObject.getString("id");
        System.out.println(id);
        //2. JSONObject轉化成自定義類物件
        PeoplePo peoplePo1 = JSONObject.parseObject(jsonString,PeoplePo.class);
        System.out.println(peoplehttp://www.cppcns.comPo1);
        //3. JSONObject轉化成Map集合
        Map map = JSONObject.parseObject(jsonString,Map.class);
        System.out.println(map);
        //4. 自定義物件轉化成json格式的字串
        PeoplePo peoplePo = new PeoplePo();
        peoplePo.setId("1");
        peoplePo.setAge(11);
        peoplePo.setName("LH");
        String peopleJson = JSON.toJSONString(peoplePo);
        System.out.println(peopleJson);
        //5. String型別轉化成JSONObject;
        String str = "{\"result\":\"success\",\"message\":\"成功!\"}";
        JSONObject jsonObject1 = JSONObject.parseObject(str);
        System.out.println(jsonObject1);
        //6. JSONObject轉化成JSONArray的兩種方式
        String str1 = "{\"result\":LYQTSgcVn\"success\",\"message\":\"成功!\",\"data\":[{\"name\":\"Tom\",\"age\":\"20\"}]}";
        JSONObject jsonToArray = JSONObject.parseObject(str1);
        //方式一
        JSONArray data = jsonToArray.getJSONArray("data");
        System.out.println(data);
        //方式二
        JSONArray jsonArray = JSONArray.parseArray(jsonToArray.getString("data"));
        System.out.println(jsonArray);
        //7. jsonArray轉化成JSONObject並取出其中的元素資料
        JSONObject o = (JSONObject) jsonArray.get(0);
        String name = o.getString("name");
        System.out.println(o);
        System.out.println(name);
        System.out.println(jsonArray.toString());
    }
}

以上為個人經驗,希望能給大家一個參考,也希望大家多多支援我們。