"{"schema":{"$schema":"http://json-schema.org/draft-07/schema#","type":"object","properties":{"a":{"type":"string","dataType":"TEXT","validation":"TEXT"},"object":{"type":"object","properties":{"fname":{"type":"string","dataType":"TEXT","validation":"TEXT"},"lname":{"type":"string","dataType":"TEXT","validation":"TEXT"}},"additionalProperties":false,"required":["fname","lname"],"dataType":"OBJECT","validation":"OBJECT"}},"additionalProperties":false,"required":["a","object"],"dataType":"OBJECT","validation":"OBJECT"},"fieldRequirements":{"name":"object1","type":"object","required":true,"subfields":[{"name":"a","type":"text","required":true,"subfields":[]},{"name":"object","type":"object","required":true,"subfields":[{"name":"fname","type":"text","required":true,"subfields":[]},{"name":"lname","type":"text","required":true,"subfields":[]}]}]}}"
This is my json string, i want to get a string from properties in above json string like this "{"a":"b","object":{"fname":"Sho","lname":"Sho"}}", any idea how to do?
i tried using field names but i m just getting fields and not able to get in hierarchy.
2
Answers
you can use a JSON parsing library such as Gson, Jackson, or JSON.simple. Here is an example using Gson:
import com.google.gson.Gson;
import java.util.List;
public class Example {
public static void main(String[] args) {
String jsonString = "[{"id":1,"name":"John"},{"id":2,"name":"Jane"}]";
}
In this example, we use the Gson library to parse the JSON string to a list of Person objects. The Person class is a simple POJO with id and name fields. After parsing the JSON string, we can iterate over the list of objects and print their fields. Note that we specify the List.class as the target class when calling gson.fromJson(), which tells Gson to parse the JSON string as a list of objects.