Reputation: 95
I need to write multiple json in one file, the problem is that every time that inserts inserted as follows:
{"1":"{\"id\":\"1\"}"**}{**"2":"{\"id\":\"2\"}"**}{**"3":"{\"id\":\"3\"}"}
and I need the chain follows to that can be read for JSON:
{"1":"{\"id\":\"1\"}"**,**"2":"{\"id\":\"2\"}"**,**"3":"{\"id\":\"3\"}"}
my code is:
public void writeMci(String mci, String nombreMci) throws Exception{
FileReadWrite a=new FileReadWrite();
JSONObject vjo = new JSONObject();
vjo.put(nombreMci, mci);
try {
BufferedWriter write=new BufferedWriter(new FileWriter("c:\\JSON.json", true));
write.write(vjo.toString());
write.flush();
write.close();
} catch (IOException e) {
//manejar error
}
System.out.print(vjo);
}
Upvotes: 0
Views: 11480
Reputation: 32953
The line you want
{"1":"{\"id\":\"1\"}"**,**"2":"{\"id\":\"2\"}"**,**"3":"{\"id\":\"3\"}"}
isn't legal either, I assume you mean (at least) something like this:
{"1":"{\"id\":\"1\"}","2":"{\"id\":\"2\"}","3":"{\"id\":\"3\"}"}
Note that this is not a JSON array, it's an object with string fields respectively called 1 2 and 3 that happen to contain JSON syntaxed string values. Although not illegal, it's highly questionable you actually want that
Now, could it be that you mean
{"1":{"id":"1"},"2":{"id":"2"},"3":{"id":"3"}}
Still an object but with object fields respectively called 1 2 and 3, where the object has just 1 field called id? Anyways (disclaimer: not an org.json library user myself - see below - so not giving any guarantees wrt the accuracy of the code :) ) :
// Create the outer container
JSONObject outer = new JSONObject();
// Create a container for the first contained object
JSONObject inner1 = new JSONObject();
inner1.put("id",1);
// and add that to outer
outer.put("1",inner1);
// Create a container for the second contained object
JSONObject inner2 = new JSONObject();
inner2.put("id",2);
// and add to outer as well
outer.put("2",inner2);
// Create a container for the third contained object
JSONObject inner3 = new JSONObject();
inner3.put("id",3);
// and add that to outer
outer.put("3",inner3);
// at this point
outer.toString()
// should return the JSON String from my last step.
if you actually want it to be an array, you need to put the objects in a JSONArray, and wrap that array as a named field inside an object. The end result will look more or less like this:
{"data": [{"id":"1"},{"id":"2"},{"id":"3"}]}
Code for the inner objects is the same as above, but there's a new layer (the array) between the inner and outer object:
JSONArray middle = new JSONArray();
...
middle.add(inner1);
...
middle.add(inner2);
...
middle.add(inner3);
...
outer.put("data",middle);
Still not sure if that's what you actually want, but I think it's a first step towards a solution. That being said, I would recommend you to switch to a nicer JSON Java library than the one from json.org you are apparently using. Look here for some feedback on different libraries that are available, for my projects I've been using Jackson since quite some time now and I'm very happy with it, but the SO question I linked to talks about several other, excellent alternatives.
Upvotes: 1
Reputation: 196
You should write both objects, as below:
write.write(vjo.toString());
write.newLine();
write.write(conb.toString());
write.newline();
write.flush();
write.close();
Upvotes: 0