在java中concat两个json对象 [英] Concat two json objects in java

查看:422
本文介绍了在java中concat两个json对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想连接多个json对象。

I want to concat multiple json object.

我有两个像这样的json

I have two json like this

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

and this

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

结果必须

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

我如何用Java做到这一点?

How can I do this in Java ?

推荐答案

执行以下操作:

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

如果您想在Java中使用JSON对象的支持,请考虑使用Gson:

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.

这篇关于在java中concat两个json对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆