skip to Main Content
"{"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


  1. 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"}]";
    
            // Parse JSON string to list of objects
            Gson gson = new Gson();
            List<Person> persons = gson.fromJson(jsonString, List.class);
    
            // Print list of objects
            for (Person person : persons) {
                System.out.println(person.getId() + " " + person.getName());
            }
        }
    
        public static class Person {
            private int id;
            private String name;
    
            public int getId() {
                return id;
            }
    
            public void setId(int id) {
                this.id = id;
            }
    
            public String getName() {
                return name;
            }
    
            public void setName(String name) {
                this.name = name;
            }
        }
    }

    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"}]";

        // Parse JSON string to list of objects
        Gson gson = new Gson();
        List<Person> persons = gson.fromJson(jsonString, List.class);
    
        // Print list of objects
        for (Person person : persons) {
            System.out.println(person.getId() + " " + person.getName());
        }
    }
    
    public static class Person {
        private int id;
        private String name;
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    }
    

    }

    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.

    Login or Signup to reply.
  2. import javax.json.Json;
    import javax.json.JsonObject;
    import javax.json.JsonReader;
    import java.io.StringReader;
    
    String json_string = "{"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":[]}]}]}}";
    
    JsonReader reader = Json.createReader(new StringReader(json_string));
    JsonObject jsonObject = reader.readObject();
    reader.close();
    
    JsonObject object = jsonObject.getJsonObject("object");
    String a = jsonObject.getString("a");
    
    JsonObject innerObject = Json.createObjectBuilder()
            .add("fname", object.getString("fname"))
            .add("lname", object.getString("lname"))
            .build();
    
    JsonObject result = Json.createObjectBuilder()
            .add("a", a)
            .add("object", innerObject)
            .build();
    
    String resultString = result.toString();
    System.out.println(resultString);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search