0

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. "28": { and "44": { is there at node position and not able to parse it.

{
  "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,
        }
      }
     }
    }
}
Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
Nayak
  • 99
  • 1
  • 7

2 Answers2

0

define 'data' as HashMap<String,String> if you are using jackson parsing

Raghavendra
  • 3,530
  • 1
  • 17
  • 18
  • Thanks for your response.but if "data" will be in hash map then all the id values like 28 , 44 .... all will be key to the hash map and anagin inside 28 and 44 offer object will be there.i am not getting this point. sorry. – Nayak Aug 17 '15 at 05:28
0

You can do this using Gson to parse Json to Java objects. Here is an example.

First you need to create a class for wrapping Request and Response objects.

public class RootWrapper {

    private Request request;
    private Response response;

    // Getters && Setters
}

Request is the easy one.

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. You must use a Map to hold "44" and "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.

import com.google.gson.annotations.SerializedName;

public class OfferWrapper {

    @SerializedName("Offer")
    private Offer offer;

    // Getters && Setters
}

And your Offer object should look like this

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.

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

By the way, your json is not valid. There are unnecessary commas at the end of url2 tags.

bhdrkn
  • 6,244
  • 5
  • 35
  • 42