3

I have json libs (json-simple) and trying to make jsonarray from jsonobjects. My aim to make JSON string look something like string below

[{"name": "somename","surname": "somesurname"},
 {"name": "somename2","surname": "somesurname2"}]

and it will have possibility of adding new objects in the same array.

This what im "hardcoding" now.

    JSONObject obj= new JSONObject();
    JSONArray arr = new JSONArray();

    for(int i = 0 ; i< arr.size() ; i++)
    {
      obj.put("name", name);
      obj.put("surname", surname);

      arr.add(obj);
      obj= new JSONObject();
    }
5
  • Your code seems to work, what exactly is your problem with it? Commented Sep 11, 2015 at 12:52
  • 1
    What's your question? Why do you use i to iterate over an empty array? Where are name and surname declared? Commented Sep 11, 2015 at 12:53
  • @still_learning name and surname are passed from the console input. why i - trying to use JSONarray like usuall array Commented Sep 11, 2015 at 13:22
  • @dhke the problem is - when im writing in json file my output is [] but. if I doesnt using construction "for" - all working fine, but every time, when adding new info creatin new array Commented Sep 11, 2015 at 13:25
  • One suggestion because you do not use Gson? Here are two examples: stackoverflow.com/questions/18993414/… and stackoverflow.com/questions/9186806/… I hope this helps. Commented Sep 11, 2015 at 14:22

0

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.