以下json的等效hashmap或list结构是什么?

时间:2014-09-19 06:09:56

标签: java json collections

需要Java对象进行JSON转换。所以我认为我需要帮助使用HashMap或List(Arraylist)来生成以下复杂的JSON。我试了一下,但是我无法生成确切的JSON格式。

{
    "uid": "prathap.b",
    "commonName": "prathap",
    "unverifiedMails": "prathap.balachandra@xyz.com",
    "alternateMobiles": "+91123456789",
    "realm": "jio",
    "deviceInfo": {
        "consumptionDeviceName": "My Desktop",
        "info": {
            "type": "browser",
            "devicePrint": {
                "screenHeight": 1080,
                "product": {
                    "sub": 20030107,
                    "name": "Gecko"
                },
                "platform": "Linuxx86_64",
                "timezone": "-¬-120",
                "userAgent": "Mozilla/5.0(X11;Linuxx86_64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/28.0.1500.5Safari/537.36",
                "screenWidth": 1920,
                "installedPlugins": "libpepflashplayer.so;internal-¬-remoting-¬-viewer",
                "longitude": 17.003768500000003,
                "latitude": 51.110937899999996,
                "installedFonts": "AbyssinicaSIL;Amiri;AsanaMath;BitstreamCharter",
                "screenColorDepth": 24
            },
            "mobile": {
                "id_brand": "ghdh",
                "model": "on_wifi"
            }
        }
    },
    "unverifiedMobiles": "+91123412341",
    "preferredOtpMethod": "M",
    "password": "rjil@1234"
}

提前致谢

PFB我试过的代码。

CreateUser createuser=new CreateUser();

CreateUser.DeviceInfo device=createuser.new DeviceInfo();

DeviceInfo.Info in=device.new Info();

Info.DevicePrint dp=in.new DevicePrint();

DevicePrint.Product prd=dp.new Product();

DevicePrint.Mobile mob=dp.new Mobile();


List l1=new ArrayList();
l1.add("123456789");
l1.add("987456321");
createuser.setAlternateMobiles(l1);

List l2=new ArrayList();
l2.add("123456789");
l2.add("987456321");
createuser.setUnverifiedMobiles(l2);

List l3=new ArrayList();
l3.add("abc@ril.com");
l3.add("cba@ril.com");
createuser.setUnverifiedMails(l3);

createuser.setPassword("password123");
createuser.setPreferredOtpMethod('M');
createuser.setRealm("jio");
createuser.setCommonName("prathap");
createuser.setUid("prathap.b");

device.setConsumptionDeviceName("My Desktop");

in.setType("browser");

dp.setInstalledFonts("A");
dp.setInstalledPlugins("B");
dp.setLatitude(123);
dp.setLongitude(456);
dp.setPlatform("C");
dp.setScreenColorDepth(789);
dp.setScreenHeight(10);
dp.setTimezone("D");
dp.setUserAgent("E");

prd.setName("john");
prd.setSub("F");

mob.setId_Brand("G");
mob.setModel("H");

List<CreateUser> listUser=new ArrayList<CreateUser>();     

listUser.add(createuser);

List<DeviceInfo> listdevice=new ArrayList<DeviceInfo>();   

listdevice.add(device);

TestInfo ti=new TestInfo();
ti.setInfo(in);

TestDevicePrint tdp=new TestDevicePrint();
tdp.setDevicePrint(dp);

Testproduct tp=new Testproduct();
tp.setProduct(prd);

List<Mobile> listmob=new ArrayList<Mobile>();   

listmob.add();

TestMobile tm=new TestMobile();
tm.setMobile(mob);

List<Object> Listfinal=new ArrayList<Object>();
Listfinal.add(createuser);
Listfinal.add(ti);
Listfinal.add(tdp);
Listfinal.add(tp);
Listfinal.add(tm);

Gson gson=new Gson();
String one =gson.toJson(Listfinal);

/*System.out.println("Lest chk"+one);*/

生成JSON:

[
    {
        "commonName":"prathap",
        "realm":"jio",
        "uid":"prathap.b",
        "password":"password123",
        "unverifiedMails":["abc@ril.com","cba@ril.com"],
        "unverifiedMobiles":["123456789","987456321"],
        "alternateMobiles":["123456789","987456321"],
        "preferredOtpMethod":"M"
    },
    {
        "info":{
            "type":"browser"
        }
    },
    {
        "devicePrint":{
            "screenColorDepth":789,
            "screenHeight":10,
            "screenWidth":0,
            "installedPlugins":"B",
            "installedFonts":"A",
            "timezone":"D",
            "latitude":123.0,
            "longitude":456.0,
            "userAgent":"E",
            "platform":"C"
        }
    },
    {
        "product":{
            "name":"john",
            "sub":"F"
        }
    },
    {
        "mobile":{
            "id_Brand":"G",
            "model":"H"
        }
    }
]

2 个答案:

答案 0 :(得分:0)

您可以将Google Gson库用于Json。这是链接click here

Gson是一个Java库,可用于将Java对象转换为JSON表示。它还可用于将JSON字符串转换为等效的Java对象。

以下是answer您的问题。

答案 1 :(得分:0)

你的问题是你把原始对象的各个组件放到一个数组中,并希望它在转换为JSON时神奇地成为一个对象。

要解决此问题,请CreateUser使用deviceInfo类型的字段DeviceInfo。同样,让DeviceInfo的字段info的类型为Info,依此类推。