简体   繁体   中英

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.

{
    "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. How to separate single json object into two json object based on json string values?

FIRST JSON OBJECT

 {
    "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

{ 
"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

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

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

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);    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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