-4

My Json response is something as below and confused how to parse it using GSON. Please have look on the following and guide me how i can parse it using GSON.

{
  "GetMICSDataResult": {
    "CONVERTIONFACT": [
{
        "CONVERSIONFACT": "1",
        "ITEMNO": "S1300W",
        "UOM": "Ea."
      },
      {
        "CONVERSIONFACT": "1",
        "ITEMNO": "S1300Y",
        "UOM": "Ea."
      }
    ],
ITEMDETAILS": [
      {
        "ITEMDESC": "FluorescentDeskLamp",
        "ITEMNO": "A11030",
        "LOCATION": "1",
        "PRICELIST": "WHS",
        "QTYONHAND": 164,
        "UNITPRICE": 38.3,
        "UOM": "Ea."
      },
      {
        "ITEMDESC": "FluorescentDeskLamp",
        "ITEMNO": "A11030",
        "LOCATION": "2",
        "PRICELIST": "WHS",
        "QTYONHAND": 247,
        "UNITPRICE": 38.3,
        "UOM": "Ea."
      }
]

} }

Community
  • 1
  • 1
Pranesh Sahu
  • 595
  • 5
  • 26

2 Answers2

2
Gson gson = new Gson();
YourClass class = gson.fromJson(jsonInString, YourClass.class);
Lucas78
  • 324
  • 3
  • 12
  • Got an error com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path – Pranesh Sahu Nov 07 '16 at 13:34
  • When your Json begins by "{" it s an object one when it begin by "[" it s an array one. As suggest by the others you should verify your structure on a json validator. – Lucas78 Nov 07 '16 at 13:40
1

There is easy way to do that. Just use POJO generator http://www.jsonschema2pojo.org/ it will give you plain object with necessary annotations. also you can use Json formatter to validate your json https://jsonformatter.curiousconcept.com/ - the JSON you posted is invalid.

Viktor Yakunin
  • 2,927
  • 3
  • 24
  • 39