Reputation: 63
I'm working in Java, and I have some JSON that is kinda like this:
{
"objectList" : [{...}, {...}, ...],
"metadata" : {...}
}
What I want to do is get the list and objects as JSON strings. So basically, I want to deserialize this JSON into an object like this:
{
"objectList" : "[{...}, {...}, ...]",
"metadata" : "{...}"
}
So I can do further processing on those strings.
What's the best way to do this?
I'm hesitant to try to use String parsing to extract the data I need since the values inside those objects may affect how it's being parsed.
Upvotes: 2
Views: 1561
Reputation: 29720
If I were you, I'd use some JSON-parsing library (such as Gson) to turn your JSON into a JsonObject
. You can then simply get the value of objectList
as a String
using JsonObject#getAsString
:
String json = "{\n" +
" \"objectList\" : \"[{...}, {...}, ...]\", \n" +
" \"metadata\" : \"{...}\" \n" +
"}";
JsonObject jsonObject = new Gson().fromJson(json, JsonObject.class);
System.out.println(jsonObject.get("objectList").getAsString());
Output:
[{...}, {...}, ...]
Upvotes: 2