Parsing json file with gson - android

I have a problem creating a mapping for a json that i want to parse using json. It's very specific, its about a json file with a json array with objects in it.
my jsonfile starts like this:
[
{
"venue": {
"venue_seasons": [
{
"created_at": "2011-12-25T23:00:28Z",
"updated_at": "2011-12-28T15:13:53Z",
"start_timestamp": 1293840000,
"id": 337,
"end": "2011-12-24T00:00:00Z",
"enabled": true,
"start": "2011-01-01T00:00:00Z",
"season_openings": [ … ],
"end_timestamp": 1324684800
},
{ … }
],
"address": "someadress",
"city": "cityname",
"name": "name",
"created_at": "2011-03-31T07:55:33Z",
etcetera
}
"venue":{another venue
So first an array, than an object (venue) with a lot of objects in it (i removed most of them, because thats not important for my question), and some arrays (like season_openings).
My parsing code works like this, im using gson. The inputstream works fine.
Reader reader = new InputStreamReader(inputStream);
JsonResponse venueResponse = gson.fromJson(reader, JsonResponse.class);
List<Venues> results = venueResponse.venue;
with the class JsonResponse:
public class JsonResponse {
public List<Venues> venue;
}
and Venues.class:
public class Venues {
public List<VenueSeasons> venue_seasons;
#SerializedName("adress")
public String getAdress;
#SerializedName("city")
public String getCity;
#SerializedName("country")
public String getCountry; etcetera
}
But when i run this code i get an error:
Unable to start activity ComponentInfo{com.hera.android.JSON/com.hera.android.JSON.TestParser2Activity}: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2
Ofcourse i can read the error: it expects an objects but get an array. I varied a lot with different jsonresponse.class and even with putting the whole json array in a json object (what is not really a solution because i need to work with this type of jsonfile). But everytime i get this or a similar error.
I think I'm close to a solution, can anyone see what i just can't and give me a helping hand?
Thanks.

Try calling Gson like this:
List<Venues> venues = gson.fromJson(reader, new TypeToken<List<Venues>>() {}.getType());
This works because your JSON document is a List, not an object that has a list as one of its properties.

Related

How to fetch multilevel JSON using volley?

My JSON Example where i am willing to get data from every "show" Array
{
"score":17.873907,
"show":{
"id":139,
"url":"http://www.tvmaze.com/shows/139/girls",
"name":"Girls",
"type":"Scripted",
"language":"English",
"genres":[
"Drama",
"Romance"
],
"status":"Ended",
"runtime":30,
"premiered":"2012-04-15",
"officialSite":"http://www.hbo.com/girls",
"schedule":{
"time":"22:00",
"days":[
"Sunday"
]
},
"rating":{
"average":6.7
},
"weight":81,
"network":{
"id":8,
"name":"HBO",
"country":{
"name":"United States",
"code":"US",
"timezone":"America/New_York"
}
},
"webChannel":null,
"externals":{
"tvrage":30124,
"thetvdb":220411,
"imdb":"tt1723816"
},
"image":{
"medium":"http://static.tvmaze.com/uploads/images/medium_portrait/31/78286.jpg",
"original":"http://static.tvmaze.com/uploads/images/original_untouched/31/78286.jpg"
},
"summary":"<p>This Emmy winning series is a comic look at the assorted humiliations and rare triumphs of a group of girls in their 20s.</p>",
"updated":1600633829,
"_links":{
"self":{
"href":"http://api.tvmaze.com/shows/139"
},
"previousepisode":{
"href":"http://api.tvmaze.com/episodes/1079686"
}
}
}
}
The Request Code I am using which is giving me the response nicely but i cannot parse the response to JSONObject
Error Showing
org.json.JSONException: Value show of type java.lang.String cannot be
converted to JSONObject
When you are passing JSONObject("show") , it is trying to convert 'show' string to json Obviously it isn't json stirng so it is throwing error;
It should be something like :
JSONObject obj = new JSONObject(response);
JSONObject obj_show= obj.getJSONObject("show"));
Log.d("show",obj_show.getJSONObject("show").toString());
The Solution Was "I Used Nested Model Class to Load Data, and that worked like a charm!"

JsonArray as empty string parsing issue with retrofit

I have a json in which 1 key is coming as jsonArray if it has data otherwise it is coming as empty string. It is giving error while parsing in gson with retrofit.
"section": "Technology",
"subsection": "",
"title": "Depiction of Amazon Stirs a Debate About Work Culture",
"abstract": "Details of working conditions at Amazon led to a response from employees, relatives and friends.",
"url": "http://www.nytimes.com/2015/08/19/technology/amazon-workplace-reactions-comments.html",
"byline": "By THE NEW YORK TIMES",
"item_type": "Article",
"updated_date": "2015-08-18T07:35:33-5:00",
"created_date": "2015-08-18T07:35:35-5:00",
"published_date": "2015-08-19T04:00:00-5:00",
"material_type_facet": "News",
"kicker": "",
"des_facet": [
"Workplace Environment"
],
"org_facet": [
"Amazon.com Inc"
],
"per_facet": "",
"geo_facet": "",
des_facet , org_facet, per_facet, geo_facet are jsonArray but you can see that 2 are not having data so coming as empty string.
How to handle this scenario with retrofit +gson.
Json format can't be changed here at server.
is there any way I can achieve it in android?
Ok so there are two option you can solve this
Option 1:
JSON which I used as a example
"des_facet": [
"Workplace Environment"
],
"org_facet": [
"Amazon.com Inc"
],
"per_facet": ["Akshay"],
"geo_facet": ""
In your model class convert those variable to Object type
#Expose
#SerializedName("geo_facet")
private Object geo_facet;
#Expose
#SerializedName("per_facet")
private Object per_facet;
then where you want to set data do the following
if (model != null)
{
if (model.getGeo_facet() != null || model.getGeo_facet() != "")
{
Object arr = model.getGeo_facet();
}
if (model.getPer_facet() !=null || model.getPer_facet()!= "")
{
Object arr = model.getPer_facet();
if (arr!=null && arr.toString().length()>0)
{
arr = arr.toString();
Log.d("akshay","arr= "+arr);
//Do your Stuff or Set data
}
}
}
This is the output= 08-11 16:51:29.830 17951-17951/com.android.example
D/akshay: arr= [Akshay]
Option 2:
Follow this which is a little bit complex
Option 3:
Write own custom Parsing like this and Handle your response accordingly
a json can have a single structure. From the code it is clear that the key is given with 2 types of data
Ideally, it should not give "" when no items. It should give null
Please change the data
If no items,
"des_facet"=null // this is the change on server side. No need to change it on app side
If it has items
"des_facet"=[
"Workplace Environment"
]
instead of
If no items,
"des_facet"=""
If it has items
"des_facet"=[
"Workplace Environment"
]
You can use AutoValue with gson plugin and mark the field as nullable which will notify the Autovalue to make this field optional.
AZs an example this is how you do it:
#AutoValue
public abstract class NewsResponse{
public static TypeAdapter<NewsResponse> typeAdapter(Gson gson){
return new AutoValue_NewsResponse.GsonTypeAdapter(gson);
}
#SerializedName("api_status")
public abstract String apiStatus();
#SerializedName("api_text")
public abstract String success();
#Nullable
#SerializedName("errors")
public abstract ErrorDetails errorDetails();
#SerializedName("news")
public abstract List<NewsDetails> newsDetails();
}
you must import both of them see more info about importing at: AutoValue and AutoValue Gson Plugin

JSON parsing with GSON with same name in objects & arrays

I have json response ,where I have the array & objects with same name. I could not parse it with GSON. Is it impossible to parse that kind of json response with Gson? or, there is some way to parse the response with GSON?
Example of response:
{
"content": [
{
"type": "text",
"content": "adnan",
"class": "32",
"align": "1"
},
{
"type": "image",
"image": "http://adnanul.jpg",
"width": 590,
"content": "sarkar",
"height": 332,
"link": "http://hashtagfail.com/post/44606137082/mobile-services-android-serialization-gson",
"caption": "3",
"class": "332"
}
]
}
The Error -
Exception is: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_ARRAY at line 1 column 26643 path $.data[1].content[27].content
The problem is that, content field inside content field is a array not a String, that is not shown inside your code example, but it is what the exception means. It could be that in some cases content is a String and in some cases an array.
Here is a similar problem and a answer -
Gson - Same field name, different types
Yes it is possible to archive this response.
public class ContentEntity {
private ArrayList<Content> content;
public class Content {
private String type;
private String image;
private int width;
private int height;
private String content;
private String link;
private String caption;
#SerializedName("class")
private String className;
// Add getter and setter here
}
}
try using below entity while parsing it using GSON.
Using default representation will propably cause this because you have keys with same name. Maybe writing an own deserializer will help.
Lets asume that you have a Java class name "Content" and the json-String represents an array of this object:
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(Content.class, new MyBrandNewContentDeserializer());
Gson gson = gsonBuilder.create();
Content[] conts = gson.fromJson(jsonInput, Content.class);
Within the deserializer you can react on the String type "content"
Try using http://www.jsonschema2pojo.org/ . Just provide your json , select JSON as source type and then select Gson as annotation style. It will generate a POJO class

How to search in JSON and return search results in activity

I am parsing a JSON in my activity to display both text and images in a gridView.However i would like to try to add search functionality on this JSON and display the results on the activity.
For example consider a JSON script on an online server like this:
[
{
"title": "Ongata Rongai",
"image": "http://snapt.t15.org/melanie/real_estates/images/house1.jpg",
"rating": 5 500 000,
"releaseYear": 2014,
"genre": ["Spacious open lounge and kitchen"]
},
{
"title": "Kisumu",
"image": "http://snapt.t15.org/melanie/real_estates/images/house2.jpg",
"rating": 1 700 000,
"releaseYear": 2014,
"genre": ["high end townhouse."]
},
{
"title": "Mombasa",
"image": "http://snapt.t15.org/melanie/real_estates/images/house3.jpg",
"rating": 68 000,
"releaseYear": 2014,
"genre": ["Its fully fitted kitchedn pantry with a beautyful backyard."]
}
]
I would like to implement search of title like Kisumu and the result to be displayed will be from this part only so that i parse this alone:
[
{
"title": "Kisumu",
"image": "http://snapt.t15.org/melanie/real_estates/images/house2.jpg",
"rating": 1 700 000,
"releaseYear": 2014,
"genre": ["high end townhouse."]
}
]
Is there a way of achieving the above?
I am open to ideas and suggestions and any help will be appreciated.
You can't search json directly. Convert the json to List Java classes. Then use Java API like Predicate to find necessary java class that satisfy your condition. For eg: Title as Kusumu as you asked.
Which will limit the list of classes to one that satisfy the condition. Then convert the java class back to json if you need Json.
Note: You can use GSON for easy class <-> json conversion
UPDATE
For converting Json to Java classes
List<ModelClass> models = new Gson().fromJson(yourJsonString, ModelClass.java);
Then using predicate
public static Predicate<ModelClass> isHavingTitle() {
return p -> p.getTitle().contains("Kusumu");
}
In Java 8 predicate filtering can be done like below in other case you can just google it. This is a sample
public static List<ModelClass> filterEmployees (List< ModelClass > models, Predicate< ModelClass > predicate) {
return models.stream().filter( predicate ).collect(Collectors.<ModelClass>toList());
}
I didn't compile any of this :)
String filteredJson = new Gson().toJson(models);
this method give resualt of server and title as you want and returns search result as JSONObject
private JSONObject SearchMethod(String result,String Key)
{
//result is your server response
//Key is Kisumu
JSONArray json = new JSONArray(result);
for(int i=0;i<json.length();i++){
JSONObject e = json.getJSONObject(i);
if(e.c.getString("title").contains(Key))
return e ;
}
return null;
}

(Android) Java: JSON Parsing

I found a lot of tutorials here, how to parse JSON Data of an JSON Array.
But my JSON File is a little bit complicate (for me). It has the following structure:
JSON File (excerpt)
{
"data": {
"schedule_id": {
"12": {
"name": "CP",
"d_id": [
"7"
]
},
"17": {
"name": "WT",
"d_id": [
"88",
"14"
]
}
}
}
}
Java Code (excerpt)
Info: I've parsed the json into "json" using HTTP GET in another Activity.
JSONObject dataJsonData = json.getJSONObject("data").getJSONObject("schedule_id");
Now I would parse through the ids using a "for"-loop:
ArrayList<String> parsedNameList = new ArrayList<String>();
for (int i = 0; i < idontknow; i++) {
String s = new Integer(i).toString();
parsedNameList.add(dateJsonData.getJSONObject(i).getString("name"));
}
This would add each value of "name" to the ArrayList.
But there are 2 problems:
1. The "schedule_id"s are messed up and incomplete. For example, there is no id "0" and, like in given json, the ids "13, 14, 15, 16" are missing.
2. The "schedule_id"s will be changed every day and will be mixed.
So I don't think, that I can use the predefined integer "i" because some integers aren't a "schedule_id". I could use this loop and would ignore empty entries in the ArrayList, but the JSON contains more than 200 ids - I think it would be more efficient, if there is another way to parse through this json.
I found some informations of the getJSONArray method, but the "d_id"s are Arrays - not the "schedule_ids".
Does anyone has an idea? Is there maybe a placeholder for the parameter of the getString method?
PS: Excuse my english, I'm from germany :)
I think this should work
Iterator keys = dataJsonData.keys();
while(keys.hasNext()) {
// loop to get the dynamic key
String currentDynamicKey = (String)keys.next();
// get the value of the dynamic key
String currentDynamicValue = dataJsonData .getString(currentDynamicKey);
parsedJsonList.add(currentDynamicValue );
}
Source: How to parse a dynamic JSON key in a Nested JSON result?
According to your context, it is better to change the json structure,if you have access to web service.
Request for json structure to be like this,
{
"data":{
"schedule":[
{
"id":12,
"name":"CP",
"d_id":[
"7"
]
},
{
"id":12,
"name":"CP",
"d_id":[
"7",
"88"
]
},
{
"id":200,
"name":"AT",
"d_id":[
"7",
"88"
]
}
]
}
}
Otherwise too much iteration can slow down you CPU.

Categories

Resources