1
[{bulkNo=91200256, cardType=CDMA_PP, denomination=300, endSerial=6048233, epfId=2254, nextSerialValue=6048226, startSerial=6048225}, {bulkNo=12050021, cardType=CDMA_PP, denomination=200, endSerial=6057151, epfId=2254, nextSerialValue=6057131, startSerial=6057131}, {bulkNo=12100005, cardType=CDMA_PP, denomination=100, endSerial=6003841, epfId=2254, nextSerialValue=6003842, startSerial=6003841}]

above shows my Gson array when i debug.need to iterate it and create objects from that

here shows my code

        Gson gson = new Gson();
        List<SerialDetails> serialDetails =new ArrayList<SerialDetails>();  
        serialDetails = gson.fromJson(sendJsonDataList, ArrayList.class);
        for(int i = 0 ; i < serialDetails.size() ; i++){
            SerialDetails item = (SerialDetails) serialDetails.get(i);//ERROR COMES IN HERE
            System.out.println(item);
        }

and here shows my error

java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to mainService.SerialDetails
    at mainService.Sales.sendJsonDataList(Sales.java:677)
    at mainService.TSR_WEB_SERVICE.sendJsonDataList(TSR_WEB_SERVICE.java:277)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)

Please help me to sort-out this issue.my bean class correctly created with getters and setters

3 Answers 3

3

Although your final solution should be working fine as you commented, you could also use a List object as in your first approach. Because, by the way, in your final solution you're creating a List that you're not using!

You just need to use a TypeToken, since List.class doesn't work as you may expect and you cannot do List<SerialDetails>.class...

So, you need something like:

Type listType = new TypeToken<List<SerialDetails>>() {}.getType();
List<SerialDetails> serialDetails = new Gson().fromJson(sendJsonDataList, listType);
//Now you can iterate over your List...
Sign up to request clarification or add additional context in comments.

Comments

1

i used with ArrayList.Class but its working Fine in My Application.

1 Comment

Dear Hareesh..I used this in my Android + KSOAP/Axis2 Web service application.its not worked perfectly. so i changed like that and now its worked fine
0

issue corrected i'm mistakenly used object arraylist instead for objectarray

        Gson gson = new Gson();
        List<SerialDetails> serialDetails =new ArrayList<SerialDetails>();  
        SerialDetails[] serialDetailsRecords = gson.fromJson(sendJsonDataList, SerialDetails[].class);
        for (SerialDetails item: serialDetailsRecords) {
             item.toString();
        }

now its works perfect

thanks for all

1 Comment

Please, since you solved, mark your question as answer and don't forget to check @MikO advice.

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.