简体   繁体   English

解析Java中的复杂json对象,该对象在一个节点中具有一些int值

[英]parse a complex json object in java which has some int value in one node

plz help me to parse below json.I am getting this json from one source but problem is it has one int value in one of the node which is item id value. 请帮助我解析下面的json。我从一个来源获取这个json,但问题是它在其中一个节点中有一个int值,即item id值。 "28": { and "44": { is there at node position and not able to parse it. "28": { and "44": {在节点位置处,无法解析。

{
  "request": {
      "Target": "some target",
    "Format": "json",
    "Service": "some service",
    "Version": "2"
  },

"response": {
    "status": 1,
    "httpStatus": 200,
    "data": {
      "28": {
        "Offer": {
          "id": "28",
          "name": "some name",
          "description":"some data",
          "url": null,
          "url2": null,
        }
      },
      "44": {
        "Offer": {
          "id": "44",
          "name": "some name",
          "description":"some data",
          "url": null,
          "url2": null,
        }
      }
     }
    }
}

如果您使用杰克逊解析HashMap<String,String>则将'data'定义为HashMap<String,String>

You can do this using Gson to parse Json to Java objects. 您可以使用Gson将Json解析为Java对象来执行此操作。 Here is an example. 这是一个例子。

First you need to create a class for wrapping Request and Response objects. 首先,您需要创建一个用于包装RequestResponse对象的类。

public class RootWrapper {

    private Request request;
    private Response response;

    // Getters && Setters
}

Request is the easy one. Request是容易的。

import com.google.gson.annotations.SerializedName;

public class Request {

    @SerializedName("Target")
    private String target;

    @SerializedName("Format")
    private String format;

    @SerializedName("Service")
    private String service;

    @SerializedName("Version")
    private String version;

    // Getters && Setters
}

And here is Response . 这是Response You must use a Map to hold "44" and "22". 您必须使用Map来容纳“ 44”和“ 22”。

import java.util.Map;

public class Response {
    private int status;
    private int httpStatus;
    private Map<String, OfferWrapper> data;

    // Getters && Setters
}

Since Offer object wrapped in an other object you must use a wrapper object. 由于Offer对象包装在另一个对象中,因此必须使用包装对象。

import com.google.gson.annotations.SerializedName;

public class OfferWrapper {

    @SerializedName("Offer")
    private Offer offer;

    // Getters && Setters
}

And your Offer object should look like this 您的Offer对象应如下所示

public class Offer {

    private String id;
    private String name;
    private String description;
    private String url;
    private String url2;

    // Getters && Setters
}

And at last, you can convert given Json to java object. 最后,您可以将给定的Json转换为java对象。

final Gson gson = new GsonBuilder().create();
final RootWrapper rootWrapper = gson.fromJson(TEST_JSON, RootWrapper.class);

By the way, your json is not valid. 顺便说一句,您的json无效。 There are unnecessary commas at the end of url2 tags. url2标记的末尾有多余的逗号。

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

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