0

This is the Json text. I can get "name,surname and books" as an Array. But the problem is books also is an array.And the content of books is coming in this form ;{"title:heresometext",paragraphs{value1:heresometext,value2:heresometext}} so it is like array in array. Like a parant-child relation. the thing i want to do is to reach thoose value1,value2,value3 strings seperated.

{"data"
 [
  "name" : "Here some text",
  "surname" : "Here some text",
  "books" : [
  {
    "title1" : "Here some text.",
    "paragraphs" : [
    {
        "value1" : "Here some text."
    },
    {
        "value2" : "Here some text."
    }
    ]
  },
  {
    "title2" : "Here some text.",
    "paragraphs" : [
    {
        "value3" : "Here some text.",
        "image1" : "Here some text."
    },
    {
        "value4" : "Here some text."
    },
    {
        "value5" : "Here some text."
    }
    ]
  }
  ]
 ]
}

How can i get them like this form, in a tree form seperated all datas to be able to save them in a DB. any help will be usefull

  name: "Here some text",
  surname: "Here some text",
  books:
         - title1 : "Here some text."
             -paragraphs:
                 - value : "Here some text.
                 - value : "Here some text.
         - title2 : "Here some text."
             -paragraphs:
                 - value : "Here some text.
                 - image : "Here some text.

0

1 Answer 1

1

We used JSON.simple in our projects. It is a JSON Parser.

Example:

public class JSONParsingExample {
  public static void main(String[] args) {
    JSONParser parser = new JSONParser();
    try {
      Object obj = parser.parse(new FileReader(parser.getClass().getClassLoader().getResource("jsondata.json").getFile()));
      JSONObject jsonObject = (JSONObject) obj;
      JSONObject data = (JSONObject) jsonObject.get("data");
      JSONArray books = (JSONArray) data.get("books");
      System.out.println(books);
      Iterator<JSONObject> iterator = books.iterator();
      while (iterator.hasNext()) {
        JSONObject book = (JSONObject) iterator.next();
        System.out.println("This books name is " + book.get("name"));
        JSONArray paras = (JSONArray) book.get("paras");
        Iterator<JSONObject> parasI = paras.iterator();
        int i = 0;
        while (parasI.hasNext()) {
          JSONObject para = (JSONObject) parasI.next();
          para.keySet().forEach(o -> System.out.println(o + "/" + para.get(o)));
        }
      }
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } catch (ParseException e) {
      e.printStackTrace();
    }
  }
}
Sign up to request clarification or add additional context in comments.

4 Comments

i am using the JSON.simple too, but i cannot reach to sub array
We can in our bachelor work, let me check tomorrow how we do it.
look, mine has 3 arrays; data[books [pharagraphs [, , , ]]]. i can reach books elements but the output is {pharagraphs [, , ,]} and not useful in this way
I adapted my example, hope this helps (Youll need JDK8+ or adapt the forEach to a for loop)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.