简体   繁体   English

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

[英]How to separate single json object into two json object based on json string values using java?

I here by mentioned the sample json object. 我在这里提到了示例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!"

}

Like above JSON object in my JSON object have more than 500 JSON string values. 像上面的JSON对象一样,我的JSON对象具有500多个JSON字符串值。 How to separate single json object into two json object based on json string values? 如何根据json字符串值将单个json对象分为两个json对象?

FIRST JSON OBJECT 第一个JSON对象

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

SECOND JSON OBJECT 第二个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...",
}

it have a pattern? 有图案吗? or it's just a random object? 还是只是一个随机对象?

I don't know its good way or bad. 我不知道它的好坏。

Create Two Model class. 创建两个模型类。

Class A 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...

}

Class B 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...

}

Now use Google GSON library to parse 现在使用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);    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM