1

I am making a call to an external webservice and they return response in json format as shown below. But when I try to parse with gson it throws an error. Also when I validate it against jsonlint.com it shows invalid json format. Now I wondering if I am doing something wrong or they are sending json data in wrong format. If its in correct format then what am I missing to parse it correctly

({"data":[["0",22247,2764,99.96,0,0],["UNDEFINED",3,1,0.04,-2.08,0]],"totalCount": 2})

Error Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 3 path $

Code JsonElement jelement = new JsonParser().parse(data);

dazzle
  • 1,346
  • 2
  • 17
  • 29

2 Answers2

1

These question can be similar like this Gson Json parser Array of Arrays

You have to create an object like:

package pruebas;

import java.util.List;

public class ResponseObject {

private List<List<Object>> data; // parse the "data":[["0",22247,2764,99.96,0,0]
private int totalCount; // parse the "totalCount": 2
public List<List<Object>> getData() {
    return data;
}
public void setData(List<List<Object>> data) {
    this.data = data;
}
public int getTotalCount() {
    return totalCount;
}
public void setTotalCount(int totalCount) {
    this.totalCount = totalCount;
}}

The you have to call the gson function in your code:

String json = "{\"data\":[[\"0\",22247,2764,99.96,0,0],[\"UNDEFINED\",3,1,0.04,-2.08,0]],\"totalCount\": 2}";
final Gson gson = new Gson();
ResponseObject response = gson.fromJson(json, ResponseObject.class);    

The data attribute have to be List<List<Object>> because you don't know what kind of object contains the json array.

Community
  • 1
  • 1
viticlick
  • 155
  • 1
  • 9
0

Try to do like this :

JsonElement jelement = new JsonParser().parse(data.substring(1, data.length()-1));

the result is :

{
  "data": [
    [
      "0",
      22247,
      2764,
      99.96,
      0,
      0
    ],
    [
      "UNDEFINED",
      3,
      1,
      0.04,
      -2.08,
      0
    ]
  ],
  "totalCount": 2
}
Maraboc
  • 10,550
  • 3
  • 37
  • 48