Concat two JSON objects in Java

I want to connect multiple JSON objects

I have two JSON like this

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

And this

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

The result must be

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

How can I do this in Java?

Solution

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 use JSON object support 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 time I'll give you what you want

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>