如何使用java根据json字符串值将单个json对象分离为两个json对象



我在这里提到了示例json对象。

{
"id": "1",
"name": "sql",
"age": 30,
"car": "car",
"office": "office",
"cancel": "CANCEL",
"ok": "OK"
"cancel": "Anulo",
"language": "en",
"remove": "Remove",
"ignore": "Ignore",
"image_list": "Image List",
"block": "Block",
"loading": "Loading...",
"splash_screen": "Splash Screen",
"save": "Save",
"skip": "Skip",
"off": "Off",
"vibration": "Vibration",
"downloading": "Downloading...",
"fix_it": "Fix it!"
}

像上面的JSON对象一样,我的JSON对象中有500多个JSON字符串值。如何根据json字符串值将单个json对象分离为两个json对象?

第一个JSON对象

{
"id": "1",
"age": 30,
"office": "office"
"cancel": "Anulo",
"remove": "Remove",
"image_list": "Image List",
"loading": "Loading...",
"save": "Save",
"fix_it": "Fix it!"
}

第二个JSON对象

{ 
"name": "sql",
"car": "car",
"cancel": "CANCEL",
"ok": "OK"
"language": "en",
"ignore": "Ignore",
"block": "Block",
"splash_screen": "Splash Screen",
"skip": "Skip",
"off": "Off",
"vibration": "Vibration",
"downloading": "Downloading...",
}

它有模式吗?或者它只是一个随机的物体?

我不知道它的好坏。

创建两个模型类。

A类

package com.test;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class A {
@SerializedName("id")
@Expose
private String id;
@SerializedName("age")
@Expose
private Integer age;
@SerializedName("office")
@Expose
private String office;
@SerializedName("cancel")
@Expose
private String cancel;
@SerializedName("remove")
@Expose
private String remove;
@SerializedName("image_list")
@Expose
private String imageList;
@SerializedName("loading")
@Expose
private String loading;
@SerializedName("save")
@Expose
private String save;
@SerializedName("fix_it")
@Expose
private String fixIt;
//Getter Setter for all members...
}

B类

package com.test;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class B {
@SerializedName("name")
@Expose
private String name;
@SerializedName("car")
@Expose
private String car;
@SerializedName("cancel")
@Expose
private String cancel;
@SerializedName("ok")
@Expose
private String ok;
@SerializedName("language")
@Expose
private String language;
@SerializedName("ignore")
@Expose
private String ignore;
@SerializedName("block")
@Expose
private String block;
@SerializedName("splash_screen")
@Expose
private String splashScreen;
@SerializedName("skip")
@Expose
private String skip;
@SerializedName("off")
@Expose
private String off;
@SerializedName("vibration")
@Expose
private String vibration;
@SerializedName("downloading")
@Expose
private String downloading;
//Getter Setter for all members...
}

现在使用Google GSON库解析

Gson gson = new Gson();
String mainJson="put your main JSON here"
A a = gson.fromJson(json, A.class);
B b = gson.fromJson(json, B.class);    

最新更新