3

I want to concat multiple json object.

I have two json like this

{"message":"test","status":"0"}

and this

{"message":"test-2","status":"1"}

result must be

[{"message":"test","status":"0"},{"message":"test-2","status":"1"}]

How can I do this in Java ?

4
  • You want to concat the values from JSON objects, or you want to create array of them ? You can refer to another link on SO: stackoverflow.com/questions/2403132/concat-multiple-jsonobjects Commented Aug 7, 2014 at 10:56
  • I can't use concat with json object.I want to create array of them Commented Aug 7, 2014 at 10:57
  • 1
    Do you want to handle them as Strings or as JSONObjects in a JSONArray? Commented Aug 7, 2014 at 10:58
  • I want to handle them as String Commented Aug 7, 2014 at 11:02

3 Answers 3

5

You can easily use JSONObject.toString() and concatenate the strings easily e.g

String x = "[" + obj1.toString() "," + obj2.toString() + "]";

or

StringBuilder newStr = new StringBuilder() ;
newStr.append("[").append(obj1.toString()).append(",").append(obj2.toString());
Sign up to request clarification or add additional context in comments.

1 Comment

missed a + between obj1.toString() and ","
3

Do something like this:

List<String> array = new ArrayList<String>();
array.add("{\"message\":\"test\",\"status\":\"0\"}");
array.add("{\"message\":\"test-2\",\"status\":\"1\"}");
array.toString();

If you want to user support for JSON object in Java, consider using Gson:

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

public class Main {

    public static void main(String[] args) {
        JsonObject json1 = (JsonObject) new JsonParser().parse("{\"message\":\"test\",\"status\":\"0\"}");
        JsonObject json2 = (JsonObject) new JsonParser().parse("{\"message\":\"test-2\",\"status\":\"1\"}");
        JsonArray array = new JsonArray();
        array.add(json1);
        array.add(json2);
        System.out.println(array.toString());
    }
}

This would give you what you want this time using objects.

8 Comments

'<>' operator is not allowed for source level below 1.7 for line 1
And generics is not allowed before 1.5! So what?!
Just do: new ArrayList<String>()
It's fixed but I have one more problem,I have to use this add method with jsonobject.But add method is only accepting string value.What can I do ?
@TolgayToklar What JSONObject are you using?
|
0
private static void concatJSON() throws IOException, InterruptedException {

JSONParser parser = new JSONParser();
Object obj = parser.parse(new FileReader(new File(Main.class.getResource("/file/user.json").toURI())));

JSONObject jsonObj = (JSONObject) obj; //usernameJsonObj

String [] values = {"0.9" , Date.from(Calendar.getInstance().toInstant()).toLocaleString()},
        innermost = {"Accomplished", "LatestDate"}, 
        inner = {"Lesson1", "Lesson2", "Lesson3", "Lesson4"};
String in = "Jayvee Villa";

JSONObject jo1 = new JSONObject();
for (int i = 0; i < innermost.length; i++)
    jo1.put(innermost[i], values[i]);

JSONObject jo2 = new JSONObject();
for (int i = 0; i < inner.length; i++)
    jo2.put(inner[i], jo1);

JSONObject jo3 = new JSONObject();
jo3.put(in, jo2);

String merger = jsonObj.toString().substring(0, jsonObj.toString().length()-1) + "," +jo3.toString().substring(1);

System.out.println(merger);
FileWriter pr = new FileWriter(file);
pr.write(merger);
pr.flush();
pr.close();
}

Comments

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.