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+",
}
asked Jan 13, 2014 at 5:11
Deepak
1951 gold badge2 silver badges6 bronze badges
2 Answers 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);
answered Jan 13, 2014 at 5:23
Mehran Hatami
13k6 gold badges31 silver badges36 bronze badges
Sign up to request clarification or add additional context in comments.
5 Comments
Sotirios Delimanolis
JSONArray does not have a put method.Mehran Hatami
check this link out and you will find the
put method.Deepak
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
Sotirios Delimanolis
array.put(obj); should be array.add(obj);Mehran Hatami
@Sotirios Delimanolis: thanks for reminding I just updated my answer. the code is the same for both except the
put and add.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.
answered Jan 13, 2014 at 5:17
Sotirios Delimanolis
281k62 gold badges718 silver badges744 bronze badges
5 Comments
Deepak
Thanks for the answer.But I didnt understand you.Can you give an example please?
Sotirios Delimanolis
@Deepak What don't you understand? An example won't help if you don't understand what you are doing.
Sotirios Delimanolis
@Deepak Good stuff. I recommend you go through the different data types that JSON supports.
Deepak
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
Sotirios Delimanolis
@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.
lang-java
JSONObject...JSONArrayand add to it what you want