简体   繁体   中英

I have a string which comprises of multiple json elements which in turn consists of multiple elements, How do I convert it to a json object

So I have a string which looks like

 [  
   {  
      "status":"Pending",
      "name":"test",
      "fileHash":"test",
      "createdOn":"tset",
      "bytesCompleted":0,
      "size":40,
      "fileId":"test"
   },
   {  
      "status":"Pending",
      "name":"test",
      "fileHash":"test",
      "createdOn":"test",
      "bytesCompleted":0,
      "size":40,
      "fileId":"tset"
   }
]

I have tried a lot of options, like gson, and org.json library but I am not able to convert that string to a JSON Object. How do I do that ?

Update: I am using Java And I am trying things like

JSONObject jsonRes = new JSONObject(res.toString());
 JSONObject jsnobject = new JSONObject(res);

 JSONArray jsonArr = new JSONArray(res);
 JSONParser parser = new JSONParser();

 JSONObject json = (JSONObject) parser.parse(res);

Mostly I am getting errors like

[ERROR]   org.json.simple.JSONArray cannot be cast to org.json.JSONObject

Also, If I do this

 JSONObject jsnobject = new JSONObject(res);
 return jsnobject;

Where res is just like the string I gave in the beggining, I am getting error like

A JSONObject text must begin with '{' at 1 [character 2 line 1]
import java.lang.reflect.Type;
import com.google.gson.reflect.TypeToken;

public static void main(String[] args) {
    String json = " [  " + 
                  "   {  " + 
                  "      \"status\":\"Pending\"," + 
                  "      \"name\":\"test\"," + 
                  "      \"fileHash\":\"test\"," + 
                  "      \"createdOn\":\"tset\"," + 
                  "      \"bytesCompleted\":0," + 
                  "      \"size\":40," + 
                  "      \"fileId\":\"test\"" + 
                  "   }," + 
                  "   {  " + 
                  "      \"status\":\"Pending\"," + 
                  "      \"name\":\"test\"," + 
                  "      \"fileHash\":\"test\"," + 
                  "      \"createdOn\":\"test\"," + 
                  "      \"bytesCompleted\":0," + 
                  "      \"size\":40," + 
                  "      \"fileId\":\"tset\"" + 
                  "   }" + 
                  "]";

    Gson gson = new Gson();
    Type listType = new TypeToken<ArrayList<Test>>(){}.getType();
    List<Test> list = gson.fromJson(json, listType);
    System.out.println(list.get(0).getStatus());        
}



public class Test {

    private String status;

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

}

You can use Jackson for this.

Create an Example Bean:

public class Example {

    @JsonProperty("status")
    private String status;
    @JsonProperty("name")
    private String name;
    @JsonProperty("fileHash")
    private String fileHash;
    @JsonProperty("createdOn")
    private String createdOn;
    @JsonProperty("bytesCompleted")
    private Integer bytesCompleted;
    @JsonProperty("size")
    private Integer size;
    @JsonProperty("fileId")
    private String fileId;

    // getters and setters
}

Parse to the List of Examples:

public List<Example> parse(String str) throws JsonParseException, JsonMappingException, IOException {
        ObjectMapper mapper = new ObjectMapper();
        return mapper.readValue(str, new TypeReference<List<Example>>() {});
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM