我有两个 JSON 格式字符串
{"user1":{"Iden":4,"nID":1},"user2":{"Iden":5,"nID":1}} // String A JSON
{"user1":{"Iden":4,"nID":1},"user3":{"Iden":6,"nID":1},"user2":{"Iden":5,"nID":1}}
在下面的程序中,这些上面的 JSON 由 Eclipse IDE 格式化
这是我的程序:
import java.util.Map;
import org.codehaus.jackson.type.TypeReference;
import com.tradeking.at.util.JsonHelper;
public class Hi {
private static JsonHelper jsonHelper = JsonHelper.getInstance();
public static void main(String[] args) throws Exception {
Map<String, Tracker> totalCusts = null;
String A = "{\"user1\":{\"Iden\":4,\"nID\":1},\"user2\":{\"Iden\":5,\"nID\":1}}";
String B = "{\"user1\":{\"Iden\":4,\"nID\":1},\"user3\":{\"Iden\":6,\"nID\":1},\"user2\":{\"Iden\":5,\"nID\":1}}";
String totalString = A+B;
if (null != totalString) {
totalCusts = (Map<String, Tracker>) jsonHelper.toObject(
totalString, new TypeReference<Map<String, Tracker>>() {
});
}
System.out.println(totalCusts);
}
}
跟踪器.java:
import org.json.JSONObject;
public class Tracker extends JSONObject{
}
当我运行上述内容时,输出为
{user1={}, user2={}}
如果我使用这个:
String totalString = B + A ;
O/p 为:
{user1={}, user3={}, user2={}}
请让我知道如何添加两个 JSON 字符串?