0

Having issues taking out data from the internal ArrayList and variables of my Json. I'm able to read in my Json into a string and print it out, but when i call fromJson and access the resulting Class, my arraylist is empty and all the data is blank.

sample codes below.

My Json string print:

{

'ShippingList': {

'ShippingInfo': {

'ShipList_No': 'SP10000001',

'Ship_Date': 'Nov-14-2017'
                                                                        },

'ItemInfo': [{

'Item_SerialNumber': 'item0000001',

'Bin_Location': '3',

'QTY': '1'

},

{

'Item_SerialNumber': 'item0000002',

'Bin_Location': '3',

'QTY': '1'

},

{

'Item_SerialNumber': 'item0000003',

'Bin_Location': '3',

'QTY': '1'

},

{

'Item_SerialNumber': 'item0000004',

'Bin_Location': '3',

'QTY': '1'

},

{

'Item_SerialNumber': 'item0000005',

'Bin_Location': '3',

'QTY': '1'

}
]
}
}

Gson Code:

Gson Gos = new Gson();
ShipList SL = Gos.fromJson(inList,ShipList.class);
String temtem = SL.getALI().get(0).getSerial();
Log.d("Proof! ","wadda " + temtem);

this prints nothing only: Proof! wadda

My classes fit what is coming out of the Json and also have been serialized. I have succesfully been able to make a class and make it into Json but i am unable to convert from Json to object properly

1
  • the above json contains error that's why GSON is not parsing it Commented Nov 28, 2017 at 4:39

1 Answer 1

0

The first problem with your code is that json uses double quotes ", not single quotes ', for containing keys and values. The first few lines of your json should be

{
  "ShippingList": {
    "ShippingInfo": {
      "ShipList_No": "SP10000001",

and so on.

I tested this as correctly formatted json with gson version 2.7 on my own machine and it worked fine

Gson Gos = new Gson();
//set inList to your input .json
ShipList SL = Gos.fromJson(inList,ShipList.class);
String temtem = SL.shippingList.itemInfo.get(0).itemSerialNumber;
System.out.println(temtem);

and it printed out item0000001. If you fix the formatting of your json and it still doesn't work some other things I would check is that inList is being correctly initialized to contain your json text.

Good Luck!

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks mate! i changed the ' to " and it begun to work like a charm. Can't believe i made such a mistake!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.