Java uses fastjson to convert String JSONObject and JSONArray to each other

  • 2021-12-11 07:35:33
  • OfStack

Directory fastjson to String, JSONObject, JSONArray to com. alibaba. fastjson. JSONObject, JSONArray and String to demo

fastjson converts to String, JSONObject and JSONArray

fastjson is Alibaba's open source JSON parsing library, which can parse JSON format strings, support the serialization of Java Bean to JSON strings, and can also deserialize from JSON strings to JavaBean

The following is mainly about the mutual installation and exchange of String, JSONObject and JSONArray that I often use in my work

String-- > > > JSONArray


String st = "[{name:Tim,age:25,sex:male},{name:Tom,age:28,sex:male},{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,age:25,sex:male}";
JSONObject rowData = JSONObject.parseObject(st);

JSONObject-- > > > JSONArray


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

Conversion between com. alibaba. fastjson. JSONObject, JSONArray and String demo

Don't say much, just go to the code


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 to object 
        String jsonString="{'name':'42313123','id':'2345','age':12}";
        JSONObject jsonObject = JSONObject.parseObject(jsonString);
        String id = jsonObject.getString("id");
        System.out.println(id);
        //2. JSONObject Convert to custom class objects 
        PeoplePo peoplePo1 = JSONObject.parseObject(jsonString, PeoplePo.class);
        System.out.println(peoplePo1);
        //3. JSONObject Convert into Map Set 
        Map map = JSONObject.parseObject(jsonString, Map.class);
        System.out.println(map);
        //4.  Custom objects are converted to json Format string 
        PeoplePo peoplePo = new PeoplePo();
        peoplePo.setId("1");
        peoplePo.setAge(11);
        peoplePo.setName("LH");
        String peopleJson = JSON.toJSONString(peoplePo);
        System.out.println(peopleJson);
        //5. String Type is converted to JSONObject;
        String str = "{\"result\":\"success\",\"message\":\" Success! \"}";
        JSONObject jsonObject1 = JSONObject.parseObject(str);
        System.out.println(jsonObject1);
        //6. JSONObject Convert into JSONArray Two ways of 
        String str1 = "{\"result\":\"success\",\"message\":\" Success! \",\"data\":[{\"name\":\"Tom\",\"age\":\"20\"}]}";
        JSONObject jsonToArray = JSONObject.parseObject(str1);
        // Mode 1
        JSONArray data = jsonToArray.getJSONArray("data");
        System.out.println(data);
        // Mode 2
        JSONArray jsonArray = JSONArray.parseArray(jsonToArray.getString("data"));
        System.out.println(jsonArray);
        //7. jsonArray Convert into JSONObject And take out the element data 
        JSONObject o = (JSONObject) jsonArray.get(0);
        String name = o.getString("name");
        System.out.println(o);
        System.out.println(name);
        System.out.println(jsonArray.toString());
    }
}

Related articles: