3

I have a json file.

{"bla":"bla"}
{"bla":"bla"}
{"bla":"bla"}
{"bla":"bla"}
......

How can I format these into a valid json type, like:

[    
    {"bla":"bla"},
    {"bla":"bla"},
    {"bla":"bla"},
    {"bla":"bla"},
    ......
    {"bla":"bla"}
]

Insert comma after each {} except last one.

How can I do that in java?

Thanks.

PS: OK. This is a json file called "1.json" which I created from TCP response. I send some names to the server and I receive response and save as a file. Because all the data is like {"bal":"bla"}{"bal":"bla"}{"bal":"bla"}{"bal":"bla"}...... This is an invalid json structure that jQuery getJSON() couldn't read it. So I want to parse it to a valid type.

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
SPG
  • 6,109
  • 14
  • 48
  • 79
  • You can do it by simple string append and replace function. – Sandeep Pathak Jul 28 '11 at 12:33
  • a text editor like vi or sed (or notepad if you only have one to do) would be more suitable for this task. in vi you type `:%s/}$/},/` (to add a comma to all lines of the file ending with `}`) – Atreys Jul 28 '11 at 12:35

6 Answers6

3
  1. Read the file line by line
  2. Use Gson to parse each JSON object in Java Object -- one by one, add it to a list
  3. Use Gson to convert the Java list in JSON array.

Look at Gson

Nishant
  • 54,584
  • 13
  • 112
  • 127
2
How can I do that in java, thx!

Don't bother. Just open it in a text editor, do a search-replace between "} and "}, and a little clean up.

Mike Thomsen
  • 36,828
  • 10
  • 60
  • 83
1

Using JSONObject.

//Java style pseudo-code (using some string reader)
JSONArray jsonArray = new JSONArray();  
while ((value = inputStream.readLine()) != null) { //This is Pseudocode - don't take is as Gospel
    JSONObject json = new JSONObject(value);
    jsonArray.put(json);
}

System.out.println(jsonArray.toString());
Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
0
String[] lines = inputJson.split('\n');
StringBuilder out = new StringBuilder();
out.append("[\n");
for (String line : lines) {
    out.append("    ").append(line).append(",\n");
}
out.setLength(out.length() - 2);
out.append("\n]");
String outputJson = out.toString();
nfechner
  • 17,295
  • 7
  • 45
  • 64
0

If you are just needing to reformat that input (lines of JSON objects, one line each), then you dont really need to convert them into Java JSON objects, you can just read the file in and convert it on the fly to the new JSON string:

StringBuilder str = new StringBuilder();
BufferedReader reader = null;
    try {
    str.append("[\n");

    for( String line = reader.readLine(); line != null; line = reader.readLine() ){
        str.append(line).append(",\n");
    }

    str.setLength(str.length()-2);
    str.append(']');

} catch( Exception ex ){
    if(reader != null){ reader.close(); }
}

or something similar. Just note that this will only work if your JSON objects are defined on single lines not spread across multiples.

Hope this helps.

cjstehno
  • 13,468
  • 4
  • 44
  • 56
  • All these data is coming from tcp response and I save them into a file. Now I want to parase them first and then save a valid json type file so that jQuery getJSON() can read it. – SPG Jul 28 '11 at 13:30
0

// you can also make a JSON String without any kind of library cause using a library extends execution time of a program.

      String json = new String("[")
    while ((value = inputStream.readLine()) != null) { 
            if(json=="["){
             json +=value
            }else{
     json +=","+value
           }
       }

  //complete the last enclosing  character
  json +="]"  

this is a simple way I think

Sagar Varpe
  • 3,531
  • 4
  • 27
  • 43