• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Mapping a api JSON object

 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,I am trying to map a response body from an api and use that response body in my app.My API returns an array of Objects.I am using retrofit to use this response body.Still not able to use it.
  I want to deserialise this data How can I do this?
 
Rancher
Posts: 99
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use org.json for this, it has JSONArray which does exactly what you want.
 
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having trouble telling if you are receiving this json on the server or in the browser.  In the browser you can use eval() to turn the json into an object, then access it by using dot and array notation.
 
priyanshi bhardwaj
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Matthew Keller wrote:I am having trouble telling if you are receiving this json on the server or in the browser.  In the browser you can use eval() to turn the json into an object, then access it by using dot and array notation.


I am receiving this data in my java file(client side) of my android and then I will be pushing it into my db and fetch that data from it and display it in my xml.
 
priyanshi bhardwaj
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Adrian Grabowski wrote:I use org.json for this, it has JSONArray which does exactly what you want.


Already done this it is throwing error
   

org.json.JSONException: Not a primitive array: class java.util.ArrayList


 
Saloon Keeper
Posts: 2679
335
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your JSON message has errors.  The are missing " symbols for the country field values.
 
priyanshi bhardwaj
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:Your JSON message has errors.  The are missing " symbols for the country field values.

Still getting same message
 
Ron McLeod
Saloon Keeper
Posts: 2679
335
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried your code and your JSON data, and it seemed to work (no exceptions):
Console output (line breaks added for clarity):
 
Ron McLeod
Saloon Keeper
Posts: 2679
335
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

priyanshi bhardwaj wrote:

Ron McLeod wrote:Your JSON message has errors.  The are missing " symbols for the country field values.

Still getting same message


Try writing response.body() to the log to verify that you are working with a properly formatted JSON payload.
 
Ron McLeod
Saloon Keeper
Posts: 2679
335
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

priyanshi bhardwaj wrote:Already done this it is throwing error ...
   org.json.JSONException: Not a primitive array: class java.util.ArrayList


Please post the entire stack trace, and the version of org.json / java-json JAR that you are using.
 
priyanshi bhardwaj
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:Try writing response.body() to the log to verify that you are working with a properly formatted JSON payload.


Already done that JSON is properly formatted.
 
priyanshi bhardwaj
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:Please post the entire stack trace,




Stack Trace is for this particular code



And How can I check JSON VERSION?
 
priyanshi bhardwaj
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:
try {
           JSONArray jsonArray=new JSONArray(responseBody);
           System.out.println("msg"+jsonArray);
       }catch (JSONException e){
           e.printStackTrace();
       }
}
}



How can I convert response.body() to string because response.body.string is no longer in use .
 
Ron McLeod
Saloon Keeper
Posts: 2679
335
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

priyanshi bhardwaj wrote:



This is the source code (or close enough) for the constructor that you are calling.  It looks like you are passing-in an Object which is not an array type.
What is the type returned by response.body()?  What is the class of the response object?

Which HTTP client library are you using?
 
Ron McLeod
Saloon Keeper
Posts: 2679
335
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

priyanshi bhardwaj wrote:


Ahh .. I see that you are using Retrofit.  I've not used it before, but from what I see, it performs the de-serialization itself (using GSON), so there is not need to use org.json or any other library to de-serialize to your entiy objects.

Here's a simple example that I wrote to give to a quick try.  It calls a service on httpbin.org which returns the HTTP headers included in the request.

Request:
Response:
Console output:
POM:
 
Ron McLeod
Saloon Keeper
Posts: 2679
335
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looking at it a bit further, if you do want to de-serialize externally, you can specify a returned type of ResponseBody. and then call the string() method to get the raw json content.

Console output:
 
priyanshi bhardwaj
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!! It is resolved now.
 
My pie came with a little toothpic holding up this tiny ad:
create, convert, edit or print DOC and DOCX in Java
https://products.aspose.com/words/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!