Java-jsonObject to jsonarray, jsonarray to list object

tags: Java foundation  java

JsonObject to JSONARRAY

JSONArray data = JSONArray.parseArray(reqMap.get("userListDto").toString());

JSONObject jsonObj = JSONObject.fromObject(resultMap.get("data"));

JSONArray certArray = JSONArray.fromObject(jsonObj.get("userListDto"));

String jsonentity = json.tojsonstring ("Data needed");
JSONObject jsonObject = JSONObject.parseObject(jsonEntity);
JSONArray jsonArray =  jsonObject.getJSONArray("userListDto");

Jsonarray to list object

The first object parameter

{"userList":[{
     "name": "zhangsan",
     "sex": 1,
     "age":10,
     "level": 1
},{
     "name": "lisi",
     "sex": 1,
     "age":13,
     "level": 4
},{
     "name": "wangwu",
     "sex": 1,
     "age":12,
     "level": 3
}
]}

 Use Fastjson's jar package

<dependency>

<groupld>com.alibaba</groupld>

<artifactld>fastjson</artifactld>

</dependency>

import com.alibaba.fastjson.JSONObject;

JSONObject jsonObj = JSONObject.parseObject(jsonObject);
String userStr = jsonObj.getString("userList");
 // array
List<User> inventoryDTOs = JSON.parseArray(userStr, User.class);
 // object
User user = JSON.parseObject(xx,User.class);

The second JSON array parameter

[{
"name": "zhangsan",
"sex": 1,
"age": 15,
"level": 6
}]

<dependency>
<groupld>net.sf.json-lib</groupld>
<artifactld>json-lib</artifactld>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;

public static void main(String[] args){
    // json to jsonarry
    JSONArray jsonArray = JSONArray.fromObject(string);
         // jsonarry transition object list
    List<User> todos = JSONArray.toList(jsonArray, new User() , new JsonConfig());
}

Below is JSONObject turning the body object, using the jar package of Fastjson

User user = JSONObject.toJavaObject(userDto,User.class);
User user = JSON.toJavaObject(userDto,User.class);

Intelligent Recommendation

Java analysis JSONObject / JSonArray

JAVA analysis jasonObject Java analysis jsonarray...

JAVA Processing JSONOBJECT JSONARRAY

In the process of learning Java, we will inevitably encounter to process JSON data formats, and now you will introduce JSON in Java. 1, handle JSONOBJECT 2, handle JSonarray Simple and easy to underst...

JSONObject and JSONArray object creation methods

1.json-lib-2.4-jdk15 and its dependent jar package Need to pay attention to the compatibility between the dependent jar packages, just importing json-lib is not enough , including the commons-logging-...

Object JSON String Map List Object JsonObject JsonArray Transform

Object JSON String Map List Object JsonObject JsonArray Transform Create an entity class Maven address Package that needs to be used JSON string turn object JSON string turns Map JSON string to turn J...

More Recommendation

JsonObject and JsonArray

JsonObject and JsonArray Simply put, the Json object is stored in the JsonArray....

jsonarray and jsonobject

        ...

JSONOBJECT / JSONARRAY

1, import dependence 2, write test class Console output:...

jsonObject,jsonArray

JSONOBJECT, JSONARRAY Dependence` JsonObject is simple to use result ``{“name”:234,“id”:123} [{“name”:234,“id”:123}]...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top