0

I have an API response as below:

[
{
    "id": "123",
    "date": "2016-12-11T12:23:34Z",
    "description": "A bag of spanners",
    "amount": "35.25",
    "currency": "GBP"
},
{
    "id": "124",
    "date": "2016-12-12T01:58:59Z",
    "description": "Hot chocholate",
    "amount": "12.50",
    "currency": "GBP"
},
{
    "id": "125",
    "date": "2016-12-12T06:11:06Z",
    "description": "Subscriptions - Magazine",
    "amount": "5.99",
    "currency": "GBP"
},
{
    "id": "126",
    "date": "2016-12-13T10:03:17Z",
    "description": "Movie rental",
    "amount": "3.99",
    "currency": "GBP"
}]

I tried parsing it using java as follows:

            Object object = (Object) HttpRequest.getInputStreamFromUrl(ApiUtils.getTransactionsUrl(), Object.class, mContext);
            Type typeMyType = new TypeToken<ArrayList<Transaction>>(){}.getType();
            Gson gson = new Gson();
            transactionList = gson.fromJson(object.toString(), typeMyType);

I'm getting 'JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Unterminated object at line 1 column 30'

I also tried some other approaches which caused com.google.gson.internal.LinkedTreeMap cannot be cast to org.json.JSONObject

How to parse this type of JSON object?

Neeraja Gandla
  • 97
  • 1
  • 6
  • 17
  • 1
    There is nothing wrong with the JSON you posted. What does `HttpRequest.getInputStreamFromUrl` return? If you got it from [this question](https://stackoverflow.com/questions/6932369/inputstream-from-a-url), it returns an `InputStream` and not a `String`, which is why your code is complaining. – Sean Bright Oct 15 '19 at 18:25
  • I guell `object.toString(),` returns not what you expected. – Jens Oct 15 '19 at 18:31
  • Agreed with @Unimportant's comment! If the returned object is an `InputStream`, you can refer to [5 ways to convert Inputstream to String in Java](http://roufid.com/5-ways-convert-inputstream-string-java/) for further parsing. – LHCHIN Oct 16 '19 at 00:40
  • getInputStreamFromUrl returns an Object. As @LHCHIN suggested I converted InputStream to String rather than Object. And then converting the string to a list of my class objects- worked. Thank you! – Neeraja Gandla Oct 16 '19 at 02:36

1 Answers1

0

I was able to solve this issue by converting the InputStream to String rather than Object. I used a method to convert InputStream to Object directly. But it doesn't seem to work at times. I used the following method to convert InputStream to String:

    public static String convertStreamToString(HttpResponse response) {
    String responseBody = null;
    HttpEntity entity = response.getEntity();
    if (entity != null) {
        try {
            responseBody = EntityUtils.toString(entity);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return responseBody;
}

And then converted the returned String to List as follows:

            String responseStr = (String) HttpRequest.getInputStreamFromUrl(ApiUtils.getTransactionsUrl(), mContext);
            Type typeMyType = new TypeToken<ArrayList<Transaction>>(){}.getType();
            Gson gson = new Gson();
            transactionList = gson.fromJson(responseStr.toString(), typeMyType);

Hope it helps someone who faces the same issue.

Neeraja Gandla
  • 97
  • 1
  • 6
  • 17