I am parsing JSON Array using Retrofit, where JSON Array looks like:
"hobbies": [ "Music", "Reading"]
Here is what my JSON looks like:
{
"type":"success",
"value":[
{
"id":1,
"title":"Title - 1",
"name":{
"first":"First - 1",
"last":"Last - 1"
},
"hobbies":[
"Writing Code - 1",
"Listening Music - 1"
]
},
.....
]
}
Value.java
private List<String> hobbies = new ArrayList<String>();
Adapter.java
viewHolder.hobbies.setText(value.getHobbies().toString());
And when I run my program, I am getting data as seen below:
[Music, Reading]
So, Question is Why I am getting [] as well in output.
Value.java
public class Value {
#SerializedName("id")
#Expose
private Integer id;
#SerializedName("title")
#Expose
private String title;
#SerializedName("hobbies")
#Expose
private List<String> hobbies = new ArrayList<String>();
#SerializedName("name")
#Expose
private Name name;
.....
public List<String> getHobbies() {
return hobbies;
}
public void setHobbies(List<String> hobbies) {
this.hobbies = hobbies;
}
}
Service.java
public interface Service {
#GET("/demo_retrofit.json")
Observable<Master> getMaster();
}
Actually value.getHobbies(); directly return an List with [ ]. better iterate it and show by using StringBuilder
MD and Jackson are right. Object.toString() method returns a string that "textually represents" this object. So for your case, hobbies is an arrayList so the textually representation will have [] with it. ;)
Edit:
You may want to use for loop for this:
for (String hobby : hobbies) {
// Do something with hobby here
}
Related
i don't arrive to get the name of the containerStatuses.
I tried this (regarding a precedent post), the error is reported on the get("name") with "The method get(String) is undefined for the type JsonElement".
Thanks for help
JsonObject data = new Gson().fromJson(myjsoncontent, JsonObject.class);
JsonArray items = data .get("items").getAsJsonArray();
for(JsonElement element : items){
JsonObject object = element.getAsJsonObject();
String containerstatusesname = object.get("status").getAsJsonObject().get("containerStatuses").getAsJsonArray().get(0).get("name").getAsString();
}
// My Json Content
{
"kind": "Space",
"apiVersion": "v1",
"metadata": {
"selfLink": "something",
"resourceVersion": "something"
},
"items": [
{
"status": {
"containerStatuses": [
{
"name": "thisismyname"
}
]
}
}
]
}
Why are you using gson emulating JSON.parse? Is using a sledgehammer to crack a nut.
If you want to use gson it's better to create a class that matches your json data as:
public class ApiResponse {
private String kind;
private String apiVersion;
private Metadata metadata;
private List<Item> items;
public List<String> getAllNames() {
List<String> allNames = new ArrayList();
for (Item item: items) {
allNames.add(item.getStatus().get(0).getName());
}
return allNames;
}
public String getFirstName() {
if (items.length == 0 || items.get(0).getStatus().length == 0) {
return "";
}
return items.get(0).getStatus().get(0).getName();
}
class Metadata {
private String selfLink;
private String resourceVersion;
}
class Item {
private List<StatusContainer> status;
List<StatusContainer> getStatus() {
return status;
}
}
class StatusContainer {
private String name;
String getName() {
return name;
}
}
}
And then execute:
ApiResponse response = gson.fromJson(myjsoncontent, ApiResponse.class);
String firstName = response.getFirstName();
And this way the response object will contain all the data of the parsed json. Notice you'll need to add the getters to access this properties if are kept private.
No need to emulate the result of JSON.parse and have JsonObject, JsonArray...
You have to change
.get(0).get("name")
to
.get(0).getAsJsonObject().get("name")
JsonArray returns JsonElements when you iterate over it
Get the Array Element as Object cause its structured as Object
.getAsJsonArray().get(0).getAsJsonObject().get("name").getAsString();
So, I have an input JSON that looks like this:
[{
"added": "2014-02-01T09:13:00Z",
"author": {
"id": "1",
"name": "George R R Martin",
"added_on": "2013-02-01T09:13:00Z"
},
"book": {
"id": "12",
"name": "Game of Thrones",
"genre": "Fantasy Fiction"
}
},
{
"added": "2015-02-01T09:13:00Z",
"author": {
"id": "2",
"name": "Patrick Rothfuss",
"added_on": "2012-09-13T011:40:00Z"
},
"book": {
"id": "15",
"name": "The Name of the Wind",
"genre": "Fantasy Fiction"
}
}, {
"added": "2016-02-01T09:13:00Z",
"author": {
"id": "2",
"name": "Patrick Rothfuss",
"added_on": "2012-09-13T011:40:00Z"
},
"book": {
"id": "17",
"name": "The Wise Man's Fear",
"genre": "Fantasy Fiction"
}
}]
I need to group it basis on author.id. An author will have one object and a list of all the books he's authored.
This is what I expect the output:
[
{
"author": "George R R Martin",
"added_on": "2013-02-01T09:13:00Z",
"books": [
{
"book_name": "Game of Thrones",
"added": "2014-02-01T09:13:00Z"
}
]
},
{
"author": "Patrick Rothfuss",
"added_on": "2012-09-13T011:40:00Z",
"books": [
{
"book_name": "The Name of the Wind",
"added": "2015-02-01T09:13:00Z"
}, {
"book_name": "The Wise Man's Fear",
"added": "2016-02-01T09:13:00Z"
}
]
}
]
I tried doing it through a normal for loop -- it works. But, just for the sake of learning more about Streams, I want to try it out using Streams.
I tried this:
Map<Author, List<Book>> collect = authorsList.stream()
.collect(Collectors.groupingBy(AuthorBookObj::getAuthor,
Collectors.mapping(AuthorBookObj::getBook, Collectors.toList())));
But, didn't get what I needed. Instead, it created three Maps instead of two.
Also tried this:
Map<AuthorTuple, List<Book>> collect = authorsList.stream()
.collect(Collectors.groupingBy(authors -> new AuthorTuple(authors.getAuthor().getId(),
authors.getAuthor().getName(), authors.getAuthor().getAddedOn()),
Collectors.mapping(AuthorBookObj::getBook, Collectors.toList())));
It also gives me three objects in the list. I expected to have two authors and corresponding books for each author.
AuthBookObj:
public class AuthorBookObj
{
private String id;
private Author author;
private Book book;
private String added;
//getter, setter
}
public class Article
{
private String name;
private String id;
private String genre;
}
public class Author
{
private String name;
private String added_on;
private String id;
}
The problem is not the way you handle the stream, it is in the equality of the objects.
The correct way is to use this code:
Map<Author, List<Book>> collect = authorsList.stream()
.collect(Collectors.groupingBy(AuthorBookObj::getAuthor,
Collectors.mapping(AuthorBookObj::getBook, Collectors.toList())));
But now you are comparing Author objects, since the objects are different you get three entries. You need to add a hashcode and equals in the Author object that will compare the objects on the author id.
//code generated from intellij.
// Author.java
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Author author = (Author) o;
return getId() == author.getId();
}
#Override
public int hashCode() {
return Objects.hash(getId());
}
If you don't have limitation on creating new POJO classes on requirement, i will do in this way
First to parse the input JSON to java object
Response class with AuthorDetails and BookDetails class
class Response {
private String addedOn;
private AuthorDetails author;
private BookDetails book;
}
AuthorDetails
class AuthorDetails {
private String id;
private String name;
private String addedOn;
}
BookDetails
class BookDetails {
private String id;
private String name;
private String gener;
}
And i will map the input json to List<Response>
List<Response> list = Arrays.asList(new Response());
Then now converting List<Response> into desired output i have added couple of POJO classes
AuthorAndBooks
class AuthorAndBooks {
#JsonProperty("author")
private String author;
#JsonProperty("added_on")
private String addedOn;
#JsonProperty("books")
List<AuthorBooks> books;
}
AuthorBooks
class AuthorBooks {
#JsonProperty("book_name")
private String name;
#JsonProperty("added")
private String added;
}
Now do group by based on author name
Map<String, List<Response>> group = list.stream().
collect(Collectors.groupingBy(res->res.getAuthor().getName()));
And now for every Author add the books
List<AuthorAndBooks> authorBooks = group.entrySet().stream().
map(entry->{
AuthorAndBooks ab = new AuthorAndBooks();
ab.setAuthor(entry.getKey());
ab.setAddedOn(entry.getValue().stream().findFirst().get().getAddedOn());
ab.setBooks(entry.getValue().stream().map(authorBook->{
AuthorBooks books = new AuthorBooks();
books.setName(authorBook.getBook().getName());
books.setAdded(authorBook.getAddedOn());
return books;
}).collect(Collectors.toList()));
return ab;
}).collect(Collectors.toList());
First of all want to pay attention to "added" field from input JSON. What does this belong to? I guess it belongs to Book object. If so it would be good to place this field (if it possible) inside Book object. Then you need to deserialize this json to java objects. It can be done by com.fasterxml.jackson.databind.ObjectMapper But you can use any json framework for this.
ObjectMapper mapper = new ObjectMapper();
AuthorBookObj[] objs = mapper.readValue(inputJson, AuthorBookObj[].class);
Then you need to group these objects and your first solution is well suited:
Map<Author, List<Book>> collect = Arrays.stream(objs)
.collect(groupingBy(AuthorBookObj::getAuthor,
mapping(AuthorBookObj::getBook, toList())));
How it was mentioned in previous answer you need to make sure there are equals/hashcode methods in your class that is used for as key in Map (In this case Author). The main confuse now is that desirable json output doesn't represent Map. It is just list of some custom object with fields like author, added_on, books which is list also.
So to achieve this goal you need to transform your Map<Author, List<Book>> to list of custom objects. For example:
public class PublicationInfo {
private String author;
private String added_on;
private List<BookBriefInfo> books;
...
}
public class BookBriefInfo {
private String book_name;
private String added;
...
}
List<PublicationInfo> infos = new ArrayList<>();
for (Map.Entry<Author, List<Book>> entry : collect.entrySet()) {
PublicationInfo info = new PublicationInfo();
info.setAuthor(entry.getKey().getName());
info.setAdded_on(entry.getKey().getAdded_on());
List<BookBriefInfo> bookInfos = new ArrayList<>();
for (Book book : entry.getValue()) {
bookInfos.add(new BookBriefInfo(book.getBook_name(), book.getAdded()))
}
info.setBooks(bookInfos);
}
Finally it can be serialized:
String jsonResult = mapper.writeValueAsString(infos);
By the way, to get json output formatted just configure it:
mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
You must override equals and hashCode. If you fail to do so, your class will violate the general contract for hashCode, which will prevent it from functioning properly in collections such as HashMap and HashSet. The Author class’s failure to override hashCode causes the two equal instances to have unequal hash codes, in violation of the hashCode contract. Add this to your Author class.
#Override
public int hashCode() {
return id.hashCode();
}
#Override
public boolean equals(Object obj) {
return obj instanceof Author && ((Author) obj).getId().equals(id);
}
With that in place, the following code snippet should work as expected.
Map<Author, List<Article>> booksByAuthor = authorsList.stream()
.collect(Collectors
.groupingBy(AuthorBookObj::getAuthor,
Collectors.mapping(AuthorBookObj::getBook, Collectors.toList())));
I have a class that should be deserialized accordingly the header request.
If header is on V1 version, ww should output the information field of Product class, like a String. Otherwise it output an Info object.
Is there another solution to do this, instead duplicate the class?
public class Product{
private String name;
private Integer id;
private Info information;
}
public class Info{
private String generalInfo;
private String fullDescription;
private String code;
}
public class Product{
private String name;
private Integer id;
private String information;
}
Above the JSON when use INFO object and when information is a string.
{
"name": "Paul",
"id": "123123,
"information": {
"generalInfo":"Business Product",
"fullDescription":"23",
"code":"9487987289929222-3"
}
}
{
"name": "Paul",
"id": "123123,
"information": "Business Product - 23 - 9487987289929222-3 "
}
This is my JSON:
{
"results": [
{
"user_id": "1",
"item_id": "18630",
"name": "Unnamed Item",
"price": "0",
"description": "",
"created_at": "2014-01-16 15:31:36",
"thumbnail": {
"image50": "http://www.example.com/adsa.jpg",
"image100": "hhttp://www.example.com/adsa.jpg"
},...
Am I doing the deserialization right?
public class ItemListModel {
private String user_id;
private String item_id;
private String name;
private String price;
private String category;
private ArrayList<ThumbnailResponse> thumbnail;
public ItemListModel(){}
// getters
}
public class ThumbnailResponse {
private String image50;
private String image100;
public ThumbnailResponse(){
}
//getters
}
I'm just confused, when do we use ArrayList, Array or List for array or object in the JSON file?
One more thing, do I need to make results as an array too if that's the case?
As you have given
"thumbnail": {
"image50": "http://www.example.com/adsa.jpg",
"image100": "hhttp://www.example.com/adsa.jpg"
}
is not a JsonArray. So you have no need to use ThumbnailResponse as an ArrayList into ItemListModel.
Your Model should be
public class ItemListModel {
private String user_id;
private String item_id;
private String name;
private String price;
private String category;
private ThumbnailResponse thumbnail; // Not array List
public ItemListModel(){}
// getters
}
And
One more thing, do I need to make results as an array too if that's
the case?
Your main data container should be contain ArrayList of ItemListModel. Like below
ArrayList<ItemListModel> results = new ArrayList<ItemListModel>();
[] in json -> array
{} in json -> object or map
in your case
// change
private ArrayList<ThumbnailResponse> thumbnail;
// to
private Map<String,String> thumbnail;
if you want it the way you declared your java object you need to provide a transformer (depends on the framework you are using)
List<ItemListModel > ItemListModel ;
try {
Type listType = new TypeToken<List<ItemListModel >>(){}.getType();
result= (List<ItemListModel >) gson.fromJson(result, listType);
} catch (Exception e) {
Log.e("Parsing exeption", e.getLocalizedMessage(), e);
}
this should work
I have a very long JSON to parse with Gson, but for brevity I have trimmed it to this example:
{
"volumes": [
{
"status": "available",
"managed": true,
"name": "va_85621143-1133-412f-83b4-57a01a552638_",
"support": {
"status": "supported"
},
"storage_pool": "pfm9253_pfm9254_new",
"id": "afb8e294-6188-4907-9f6f-963c7623cecb",
"size": 9
},
{
"status": "in-use",
"managed": false,
"name": "bt_newd20",
"support": {
"status": "not_supported",
"reasons": [
"This volume is not a candidate for management because it is already attached to a virtual machine. To manage this volume with PowerVC, select the virtual machine to which the volume is attached for management. The attached volume will be automatically included for management."
]
},
"storage_pool": "KVM",
"mapped_wwpns": [
"2101001B32BD4280",
"2100001B329D4280",
"2101001B32BD637E",
"2100001B329D637E"
],
"id": "c7838c79-17ca-3cbc-98e5-3567fde902d8",
"size": 0
},
{
"status": "available",
"managed": true,
"name": "vdisk138",
"support": {
"status": "supported"
},
"storage_pool": "Chassis2_IBMi",
"id": "b6d00783-9f8c-40b8-ad78-956b0299478c",
"size": 100
}
]
}
From SO and few other places, I have found that I need to define a top level container like one below but I do not know how to complete its definition
static class VolumeContainer {
//I don't know what do in here. This is the first problem
}
and then a class for each Volume
static class Volume {
private String status;
private boolean managed;
private String name;
//This is the second problem.The "support" variable should not be a string.
//It is in {}. Just for information, I won't use it.
//private String support;
private String storagePool;
private List<String> mapped_wwpns;
private String id;
private String size;
}
I am trying to parse it and this is what I coded so far:
JsonParser parser = new JsonParser();
JsonObject obj = parser.parse(response).getAsJsonObject();
Gson gson = new Gson();
The JSON string is stored in a variable named response
VolumeContainer vc = gson.fromJson(response,VolumeContainer.class);
My final requirement is a HashTable of id and associated name.
First problem: your VolumeContainer needs to be:
public class VolumeContainer {
public List<Volume> volumes;
}
it does not need to be static.
Second problem: your Volume class should be like this:
public class Volume {
private String status;
private Boolean managed;
private String name;
private Support support;
private String storage_pool;
private String id;
private int size;
private List<String> mapped_wwpns;
public String getId(){return id;}
public String getName(){return name;}
}
I defined a class named Support like this:
public class Support {
private String status;
private List<String> reasons;
}
Third problem: parsing, If response string contains your example data, simply parse like this:
Gson g = new Gson();
VolumeContainer vc = g.fromJson(response, VolumeContainer.class);
Fourth problem: get the map. Finally to get your HashMap, just do like this:
HashMap<String, String> hm = new HashMap<String,String>();
for(Volume v: vc.volumes){
hm.put(v.getId(), v.getName());
}