简体   繁体   中英

I have a data String with multiple JSON Objects, how can I store all JSON Objects from the String in an Array filled with objects?

For example if I had a string of 3 json objects:

{"id":"1","status":"SENT","sent_at":"2020-02-25T11:00:02+00:00"},{"id":"2","status":"SENT","sent_at":"2020-02-18T11:00:02+00:00"},{"id":"3","status":"SENT","sent_at":"2020-02-11T11:00:03+00:00"}

(above example is one long string)

How can I store all above information in an Array of individual json objects? I'm using Java!

Step 1:

Convert to valid json by wrapping in [...] , ie (formatted for readability only):

[
    {"id":"1","status":"SENT","sent_at":"2020-02-25T11:00:02+00:00"},
    {"id":"2","status":"SENT","sent_at":"2020-02-18T11:00:02+00:00"},
    {"id":"3","status":"SENT","sent_at":"2020-02-11T11:00:03+00:00"}
]

Step 2:

Parse using your favorite library, eg

List<Object> objects = new ObjectMapper().readValue(str, List.class);

If you deperately need an array (not recommended):

Object[] array = objects.toArray();

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