divyanshu gupta
divyanshu gupta

Reputation: 137

how to serialize a specific json format

I need to set json in a format provided below.

"Tyre": {
    "title": "Tyre",
    "cls": "TyreCondition",
    "items": [
      {
        "firstItem": [
          {
            "title": "Front right tyre",
            "value": "50%",
            "subValue": "30,000 km Approx"
          }
        ],
        "secItem": [
          {
            "title": "title",
            "value": "Front right tyre tread - 50%"
          },
          {
            "title": "title",
            "value": "Front right tyre tread - 50%"
          }
        ]
      }
    ]
  }

class that i have created for this is looks like:

private String title;
private String cls;



@SerializedName("firstItem")
private List<UsedCarInspectionInfo> items;
@SerializedName("secItem")
private List<UsedCarTyreImage> imageList;
//getter and setter

when i run this code with this class structure, I am getting the json like-

"Tyre": {
"title": "Tyre",
"cls": "TyreCondition",
"firstItem": [
  {
    "title": "Front right tyre",
    "value": "50%",
    "subValue": "30,000 km Approx"
  }
],
"secItem": [
  {
    "title": "title",
    "value": "Front right tyre tread - 50%"
  },
  {
    "title": "title",
    "value": "Front right tyre tread - 50%"
  }
]}

Any idea how i can get the firstItem and secItem array in Items array?

Upvotes: 0

Views: 92

Answers (1)

Benjamin M
Benjamin M

Reputation: 24567

You get no items key in your JSON, because you override the items field in your Java class with @SerializedName.

You need something like this:

String title;
String cls;
List<Item> items;


static class Item {
  List<FirstItem> firstItem;
  List<SecItem> secItem;
}

static class FirstItem {
  String title;
  String value;
  String subValue;
}

static class SecItem {
  String title;
  String value;
}

Upvotes: 2

Related Questions