0

I was looking this for creating JSON and the output is

{ "age":100, "name":"mkyong.com", "messages":["msg 1","msg 2","msg 3"] }

But i want an array of 10 times like this

{
        "engine": "Trident",
        "browser": "Internet Explorer 4.0",
        "platform": "Win 95+",

    },
    {
        "engine": "Trident",
        "browser": "Internet Explorer 5.0",
        "platform": "Win 95+",

    },
    {
        "engine": "Trident",
        "browser": "Internet Explorer 5.5",
        "platform": "Win 95+",

    },

And this is the way I tried

import java.io.FileWriter;
import java.io.IOException;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

public class TestJson {
     public static void main(String[] args) {
        JSONObject obj=null;
         obj = new JSONObject();
    for(int i=0;i<10;i++)
    {

    obj.put("engine", "mkyong.com");
    obj.put("browser", i);
obj.put("platform", i);



    //obj.put("messages", list);
    }
    try {

        FileWriter file = new FileWriter("c:\\test.json");
        file.write(obj.toJSONString());
        file.flush();
        file.close();

    } catch (IOException e) {
        e.printStackTrace();
    }

    System.out.print(obj);

     }

}

but this only prints 1 json

{
        "engine": "Trident",
        "browser": "Internet Explorer 4.0",
        "platform": "Win 95+",

    }
4
  • You want a JSON array but you are using a JSONObject... Commented Jan 13, 2014 at 5:12
  • @SotiriosDelimanolis Thanks for the reply.How to create 10 times like the example Commented Jan 13, 2014 at 5:14
  • @Deepak as suggested: create JSONArray and add to it what you want Commented Jan 13, 2014 at 5:19
  • I've done the same in C# if you will say i can explain the logic in the answer. the logic is like create a list of object and then add an new item to the list each and every time. Commented Jan 13, 2014 at 5:34

2 Answers 2

2

you can do this:

JSONObject jsonObject = new JSONObject();
JSONArray array = new JSONArray();
for(int i=0;i<10;i++){
    JSONObject obj = new JSONObject();
    obj.put("engine", "mkyong.com");
    obj.put("browser", i);
    obj.put("platform", i);

    //if you are using JSON.simple do this
    array.add(obj);

    //and if you use json-jena
    array.put(obj);
}
jsonObject.put("MyArray" , array);

System.out.print(jsonObject);
Sign up to request clarification or add additional context in comments.

5 Comments

JSONArray does not have a put method.
check this link out and you will find the put method.
yes it does not have a put method but when i changed it to add,it worked.Sorry i do not have enough reputations to upvote
array.put(obj); should be array.add(obj);
@Sotirios Delimanolis: thanks for reminding I just updated my answer. the code is the same for both except the put and add.
0

In the following code

JSONObject obj=null;
obj = new JSONObject();
for(int i=0;i<10;i++)
{

    obj.put("engine", "mkyong.com");
    obj.put("browser", i);
    obj.put("platform", i);

    //obj.put("messages", list);
}

you are creating a single JSONObject and overwriting its values 10 times.

Why are you working with a JSONObject when you want a JSON array?

Create a JSONArray and add 10 JSONObject objects to it.

5 Comments

Thanks for the answer.But I didnt understand you.Can you give an example please?
@Deepak What don't you understand? An example won't help if you don't understand what you are doing.
@Deepak Good stuff. I recommend you go through the different data types that JSON supports.
I do not have enough reputations to upvote your answer but I promise I will do when I have it later.Thanks again for the answer
@deepak Don't worry about that. But please don't ask for solutions right off the bat. Try to understand what you are doing and what you are trying to do. The solution will often come on its own.

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.