Get value of Json With Gson - java

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();

Related

How do I deserialize array of different types with GSON

First, I know there are many questions on this topic but I couldn't find one that solves my problem.
I need to deserialize with Gson a json that is in this form:
{
"name": "abc",
"entries":
[
[
"first_entry_name",
{"is_ok": true, "type": "first_entry_type"}
],
[
"second_entry_name",
{"is_ok": false, "type": "second_entry_type"}
]
]
}
I've implemented the classes:
class Entries
{
String name;
ArrayList<Entry> entries;
}
class Entry
{
String name;
Details details;
}
class Details
{
Boolean is_ok;
String type;
}
I'm deserializing with:
Entries ent = new Gson().fromJson(json, Entries.class);
And I'm getting this error:
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY
I understand why I'm getting this error but I can't think of a way to deserialize this json.
How should this json be deserialized?
Code is fine but your JSON file should be like below
{
"name": "abc",
"entries":
[
{
"first_entry_name":{"is_ok": true, "type": "first_entry_type"}
},
{
"second_entry_name":{"is_ok": false, "type": "second_entry_type"}
}
]
}
In the json posted in original question, there was list inside a list (entries) but ideally it should be json element inside a list.
Here is the screenshot of code with output
Hope this helps
i am not really sure if that help , i come from c# world . but i think that will be a generic problem , so try to cast your instance (ent) from object to array or list of that Type ..
i mean like this
this c# syntax but i think there are mostly the same with java
<List>Entries ent = (<List>Entries)new Gson().fromJson(json, Entries.class);
it can be also here Array but List is more dynamic .. hopefully works
You may do something like below.
try adding another wrapper class as I have added sample here as EntryTwo.
class Entries
{
String name;
ArrayList<Entry> entries;
}
class Entry
{
ArrayList<EntryTwo> entryTwo;
}
class EntryTwo
{
String name;
Details details;
}
class Details
{
Boolean is_ok;
String type;
}
Hope this works...
You need to implement custom deserialiser for List<Entry> type:
class EntriesJsonDeserializer implements JsonDeserializer<List<Entry>> {
#Override
public List<Entry> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
JsonArray array = json.getAsJsonArray();
List<Entry> entries = new ArrayList<>(array.size());
for (JsonElement item : array) {
JsonArray itemArray = item.getAsJsonArray();
entries.add(parseEntry(context, itemArray));
}
return entries;
}
private Entry parseEntry(JsonDeserializationContext context, JsonArray array) {
Entry entry = new Entry();
entry.setName(array.get(0).getAsString());
entry.setDetails(context.deserialize(array.get(1), Details.class));
return entry;
}
}
You can register it using JsonAdapter annotation:
class Entries {
String name;
#JsonAdapter(EntriesJsonDeserializer.class)
List<Entry> entries;
}
You can try following code:
public class Entries {
private String name;
private List<List<String>> entries;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setEntries(List<List<String>> entries) {
this.entries = entries;
}
public List<List<String>> getEntries() {
return entries;
}
}

How to get the data from the JSON list of JSON list

I've got an JSON string from my API, looks like this:
[
{
"id": "abc",
"data": {
"Name": "Peter",
"Date": "2017/12/01"
}
},
{
"id": "def",
"data": {
"Name": "Tina",
"Date": "2017/12/20"
}
},
{
"id": "ghi",
"data": {
"Name": "Amy",
"Date": "2017/12/16"
}
}
]
Then, I use (java):
Gson gson = new Gson();
Type resultType = new TypeToken<List<Map<String, Object>>>() {
}.getType();
List<Map<String, Object>> result = gson.fromJson(info, resultType);
if I call result.get(0).toString());
then it returned:
{id=abc, data={Name=Peter, Date=2017/12/01}}
if I call result.get(0).get("id").toString();
then it returned
abc
Now I want to get the data of "data", when I call result.get(0).get("data").toString();
then it returned
{Name=Peter, Date=2017/12/01}
Finally I want to get the "Name" info, but when I tried to convert this string to Map, it cause some problem, the code is like this:
Type type = new TypeToken<Map<String, Object>>(){}.getType();
Map<String, Object> myMap = gson.fromJson(str, type);
This doesn't work. I found that maybe the string is not a general type of JSON, it is like "Name=Peter, Date=2017/12/01", but it needs "Name": "Peter", "Date": "2017/12/01" , right? Is that the problem? How can I get the data of Name? Can anyone help me?
Updated:
I found that if "Name" = "", then I couldn't get it as string type, I cannot use "data.get("Name");". But I still need it. Anyone can fix it? Thanks.
You can directly convert the response into the POJO/Model class. Check this and this
You don't need manual parsing, if you are using Gson. See how-
List<Response> responseList = new Gson().fromJson(yourJson, new TypeToken<List<Response>>() {
}.getType());
Data data = responseList.get(0).getData();
String id = responseList.get(0).getId();
String date = data.getDate();
String name = data.getName();
Isn't this magic? No manual parsing at all.
Response.java class
public class Response {
private Data data;
private String id;
public void setData(Data data) {
this.data = data;
}
public Data getData() {
return data;
}
public void setId(String id) {
this.id = id;
}
public String getId() {
return id;
}
}
Data.java class
public class Data {
private String date;
private String name;
public void setDate(String date) {
this.date = date;
}
public String getDate() {
return date;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
How to generate Pojo classes? So here is several websites jsonschema2pojo. Also many Android Studio plugins available, I use RoboPOJOGenerator.
First of all, your JSON is malformed, it shouldn't have a comma after date.
and to answer your question, don't use map at all.
If you really want to do it without creating a model and additional classes, do it this way:
Gson gson = new Gson();
Type resultType = new TypeToken<List<JsonObject>>() {}.getType();
List<JsonObject> result = gson.fromJson(info, resultType);
System.out.println(result.get(0).get("data").toString());
JsonObject data = result.get(0).get("data").getAsJsonObject();
System.out.println(data.get("Name"));

How to create a JSON array with this specific structure?

I intend to create a JSON Array with the following structure. The metadata tag is going to constant in all the entries. I am stumped.
[{
"metadata": {
"Value": "String"
},
"name": "String",
"id": "String"
},
{
"metadata": {
"Value": "String"
},
"name": "String",
"id": "String"
}
]
public class yourJsonObject {
private Map<String, String> metadata;
private String name;
private string id;
public yourJsonObject() {
}
public Map<String, String> getMetadata(){
return metadata;
}
public void setMetadata(Map<String, String> metadata){
this.metadata = metadata;
}
public String getName(){
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId(){
return id;
}
public void setId(String id){
this.id = id;
}
}
Then somewhere else you can just do this:
ObjectMapper mapper = new ObjectMapper(); // create once, reuse
yourJsonObject example = new yourJsonObject(); // have your POJO you want to save
mapper.writeValue(new File("result.json"), example);
To read you can just use:
ObjectMapper mapper = new ObjectMapper(); // create once, reuse
yourJsonObject value = mapper.readValue(new File("data.json"), yourJsonObject .class);
Both snippets are taken from my linked wiki article from jackson themselves.
Jackson should automatically be able to parse this POJO to an equivalent JSON if configured correctly.
Note: Jackson has to be globally registered and has to know about it. Please read the wiki of what you use to know about it... Jackson in 5 Minutes
Else you could just manually build the JSON like Neeraj said.
JSONArray array = new JSONArray(); // Create JSONArray Object
JSONObject jsonObject = new JSONObject(); // Your JSONObject which gets added into array
jsonObject.put("metadata",new MetaDataCustomClass("SomeRandomStringValue"));
jsonObject.put("name", "Neeraj");
jsonObject.put("id", "123");
array.add(jsonObject); // Here you push the jsonObject into Array.
Note: MetaDataCustomClass is just a custom Class having a Value instance variable of type String.
Class MetaDataCustomClass {
private String value;
public MetaDataCustomClass(String value){
this.value = value;
}
}

Parsing Json Object to Json Array using Gson

I am calling a webservice which gives me a json like this
{
"discussions": [{
"id": 54,
"name": "Test Discusssion",
"discussion": 41,
"created": 1472816138,
"modified": 1472816138,
"subject": "Test Discusssion",
"message": "<p>Welcome all to test discussion<\/p>",
}],
"warnings": []
}
But in android I am parsing it as
ArrayList<MoodleDiscussion> mDiscussions = gson.fromJson(reader,
new TypeToken<List<MoodleDiscussion>>() {
}.getType());
And the error I am getting is
java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT
I want to convert the received json into an array how should I ?
Here is the MoodleDiscussion class
public class MoodleDiscussion extends SugarRecord < MoodleDiscussion > {#
SerializedName("id") int discussionid;#
SerializedName("name") String name;#
SerializedName("subject") String subject;
public int getDiscussionid() {
return discussionid;
}
public String getName() {
return name;
}
public int getUserid() {
return userid;
}
public String getSubject() {
return subject;
}
}
The list you're trying to parse is contained within a nested json data structure that also needs to be represented in java classes if Gson is to parse it correctly.
You'll need a container class that looks something like this:
public class MoodleDiscussionResponse {
private List<MoodleDiscussion> discussions;
private List<Object> warnings;
public List<MoodleDiscussion> getDiscussions() {
return discussions;
}
public List<Object> getWarnings() {
return warnings;
}
}
Then you should be able to read it like so:
MoodleDiscussionResponse response = gson.fromJson(reader, MoodleDiscussionResponse.class);
List<MoodleDiscussion> mDiscussions = response.getDiscussions();
Define class the same:
class Discussion implements Serializable
{
#SerializedName("id")
int id;
#SerializedName("name")
String name;
#SerializedName("discussion")
String discussion;
bla bla
}
and:
class ResultResponse implements Serializable
{
#SerializedName("discussions")
List<Discussion> mDiscussion = new ArrayList<>();
}
I think that is enough to parser using gson.
You can not using below code in this case :)
ArrayList<MoodleDiscussion> mDiscussions = gson.fromJson(reader,
new TypeToken<List<MoodleDiscussion>>() {
}.getType());
you can parse in two ways first get array list of json object and loop it
JSONArray discussionsList = obj1.getJSONArray("discussions");
for (int i = 0; i < discussionsList.length(); i++) {
JSONObject jsonObject = orderList.getJSONObject(i);
Type type = new TypeToken<MoodleDiscussion>() {
}.getType();
CompletedOrderData completedOrderData = new Gson().fromJson(jsonObject.toString(), type);
disscussionDataArrayList.add(completedOrderData);
and second way is parse jsonArray object as below
Type type = new TypeToken<ArrayList<MoodleDiscussion>>() {
}.getType();
disscussionDataArrayList=new Gson().fromJson(orderList,type);

Parse Json objects with different types using gson

I have the following json from the Server. It is a json array with different objects. I want to identify the user objects based on the key "type" and add them to a user hashmap and fetch user to show information in my view containing the "payments" object. I am using gson and retrofit. TIA
"included":[
{
"id":"1",
"type":"payments",
"attributes":{
"amount_cents":100,
"amount_currency":"INR",
"description":"Test description!!",
"created_at":"2016-03-01T11:30:53Z",
"status":"paid",
"paid_at":null,
"charged_at":null,
"formatted_amount":"Rs1.00"
},
"relationships":{
"sender":{
"data":{
"id":"2",
"type":"users"
}
},
"receiver":{
"data":{
"id":"1",
"type":"users"
}
}
}
},
{
"id":"2",
"type":"users",
"attributes":{
"first_name":"Rob",
"last_name":"Thomas"
}
},
{
"id":"1",
"type":"users",
"attributes":{
"first_name":"Matt",
"last_name":"Thomas"
}
}]
My classes are
public class ActivityFeedItem implements IFeedItem {
#SerializedName("id")
String id;
#SerializedName("type")
String type;
#SerializedName("attributes")
Attributes attributes;
protected class Attributes {
double amount_cents;
String amount_currency;
String description;
String created_at;
String status;
String paid_at;
String charged_at;
String formatted_amount;
Relationships relationships;
public double getAmount_cents() {
return amount_cents;
}
public String getAmount_currency() {
return amount_currency;
}
public String getDescription() {
return description;
}
public String getCreated_at() {
return created_at;
}
public String getStatus() {
return status;
}
public String getPaid_at() {
return paid_at;
}
public String getCharged_at() {
return charged_at;
}
public String getFormatted_amount() {
return formatted_amount;
}
public Relationships getRelationships() {
return relationships;
}
}
}
and
public class UserFeedItem implements IFeedItem {
#SerializedName("id")
String id;
#SerializedName("type")
String type;
#SerializedName("attributes")
Attributes attributes;
public class Attributes {
#SerializedName("first_name")
String first_name;
#SerializedName("last_name")
String last_name;
}
}
This is pretty easy if you just put your JSON response String into a JSONArray. Then you can just access the type field and test if it's users. Like this:
JSONArray jsonArray = new JSONArray(yourServerResponseString);
for(int i=0; i<jsonArray.length(); i++) {
JSONObject object = (JSONObject) jsonArray.get(i);
String type = object.getString("type");
if(type.equals("users")) {
//add to your users HashMap
}
}
First of all create an array of objects from you JSON using GSON as below
Gson gson= new Gson();
String jsonString= yourJsonObject.getString("included");
ActivityFeedItem[] activityFeedArray=gson.fromJson(jsonString,ActivityFeedItem[].class);
Now your activityFeedArray contains all the feedItems you get in JSON. Then you can iterate through it as you would in any array and add to hashmap when type is user as below-
for(ActivityFeedItem item:activityFeedArray) {
if(item.type.equals("users")) {
//add to your users HashMap
}
}

Categories