query of json in flutter - android

I've got the following json response from spotify api:
{
"artists" : {
"href" : "https://api.spotify.com/v1/search?query=U2&type=artist&offset=0&limit=20",
"items" : [ {
"external_urls" : {
"spotify" : "https://open.spotify.com/artist/51Blml2LZPmy7TTiAg47vQ"
},
"followers" : {
"href" : null,
"total" : 3146466
},
"genres" : [ "irish rock", "permanent wave", "rock" ],
"href" : "https://api.spotify.com/v1/artists/51Blml2LZPmy7TTiAg47vQ",
"id" : "51Blml2LZPmy7TTiAg47vQ",
"images" : [ {
"height" : 640,
"url" : "https://i.scdn.co/image/e22d5c0c8139b8439440a69854ed66efae91112d",
"width" : 640
}, {
"height" : 160,
"url" : "https://i.scdn.co/image/7293d6752ae8a64e34adee5086858e408185b534",
"width" : 160
} ],
"name" : "U2",
"popularity" : 76,
"type" : "artist",
"uri" : "
I would like to get the id value, which in this case is
51Blml2LZPmy7TTiAg47vQ
but I can't figure out how to make a query in this json using dart.
I tried out as this answer but did not work.
Is there any way to make a query on json with flutter?

Try json.decode(string)['artists']['items'][0]['id']
json.decode decodes the string to a Map<dynamic, dynamic> containing nested Maps and Lists into which you can index. (Maps are indexed by key; Lists are indexed by integer.)

Related

Android: Parse very large complex JSON with Gson

In my application I am downloading very large, complex, JSON files (over 100MB) from the server. The structure of these files can differ and I don't always know the key names. Because of this I cannot create a custom object to hold the data. The one thing that I do always know is that the file contains an array of objects. What I need to do is convert each object into a JsonObject and add it to a Kotlin List to be used in a RecyclerView and other places throughout the app.
What I do currently is download the JSON as a Reader object using OKHttp like this:
val jsonStream = response.body!!.charStream()
From there I use Gson's JsonReader to iterate through the file and create my JSON objects.
val array = mutableListOf<JsonObject>()
JsonReader(jsonStream).use {reader ->
reader.beginArray()
while (reader.hasNext()) {
val json = JsonParser.parseReader(reader).asJsonObject
array.add(json)
}
}
Here is an example of what an object looks like:
{
"employee" : [
{
"person_id" : 1441815,
"id" : 1441815,
"first" : "Steve",
"last" : "Eastin",
"custom_id" : 3916884,
"middle" : ""
},
{
"person_id" : 1470429,
"id" : 1470429,
"first" : "Kerry",
"last" : "Remsen",
"custom_id" : 3916884,
"middle" : ""
},
{
"person_id" : 1471551,
"id" : 1471551,
"first" : "Clu",
"last" : "Gulager",
"custom_id" : 3916884,
"middle" : ""
},
{
"person_id" : 1604199,
"id" : 1604199,
"first" : "Brian",
"last" : "Wimmer",
"movie_custom_id" : 3916884,
"middle" : "",
"job" : "actor"
},
{
"person_id" : 1632559,
"id" : 1632559,
"first" : "Lyman",
"last" : "Ward",
"movie_custom_id" : 3916884,
"middle" : "",
"job" : "actor"
},
{
"person_id" : 1788526,
"id" : 1788526,
"first" : "Christie",
"last" : "Clark",
"custom_id" : 3916884,
"middle" : ""
},
{
"person_id" : 1869213,
"id" : 1869213,
"first" : "Sydney",
"last" : "Walsh",
"custom_id" : 3916884,
"middle" : ""
},
{
"person_id" : 1892343,
"id" : 1892343,
"first" : "Robert",
"last" : "Rusler",
"custom_id" : 3916884,
"middle" : ""
},
{
"person_id" : 1961713,
"id" : 1961713,
"first" : "Jack",
"last" : "Sholder",
"custom_id" : 3916884,
"middle" : ""
},
{
"person_id" : 2476997,
"id" : 2476997,
"first" : "Tom",
"last" : "McFadden",
"custom_id" : 3916884,
"middle" : ""
},
{
"person_id" : 3401109,
"id" : 3401109,
"first" : "Allison",
"last" : "Barron",
"custom_id" : 3916884,
"middle" : ""
},
{
"person_id" : 8201549,
"id" : 8201549,
"first" : "JoAnn",
"last" : "Willette",
"custom_id" : 3916884,
"middle" : ""
},
{
"person_id" : 27936448,
"id" : 27936448,
"first" : "Melinda",
"last" : "Fee",
"custom_id" : 3916884,
"middle" : "O."
},
{
"person_id" : 40371176,
"id" : 40371176,
"first" : "Steven",
"last" : "Smith",
"custom_id" : 3916884,
"middle" : ""
},
{
"person_id" : 45323542,
"id" : 45323542,
"first" : "Kimberly",
"last" : "Lynn",
"custom_id" : 3916884,
"middle" : ""
},
{
"person_id" : 45323546,
"id" : 45323546,
"first" : "Jonathan",
"last" : "Hart",
"custom_id" : 3916884,
"middle" : ""
}
],
"id" : 3916884,
"array1" : [
"3",
"4",
"5"
],
"date_added" : "2020-10-10 15:26:09",
"number1" : 1985,
"verified" : 0,
"number2" : 14446757,
"string1" : "test string 1",
"null1" : null,
"string2" : "test string 2",
"null2" : null,
"number3" : 0,
"array2" : [
"1",
"2",
"3"
],
"string3" : "test string 3",
"string4" : "test string 4",
"null3" : null,
"null4" : null,
"number4" : 1
}
My JSON files can contain 10,000+ objects. The issue I am having is that I'm running out of memory. Through a lot of testing I've determined that it is because of the nested array of employee objects. Is there a way to parse this file more efficiently, to prevent running out of memory, or am I going to have to come up with a different solution to handle this amount of data?

Android OkHTTP returning inconsistent String?

I am using OkHTTP to simply get text from the body of a url:
class DownloadUrl {
String readUrl(String myUrl) throws IOException,NullPointerException {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(myUrl)
.build();
Response response = client.newCall(request).execute();
return response.body().string();
}
}
However, I am getting inconsistent String, despite the URL and its contents being same every time. The some returned string leave out blocks of the JSON for example, I should be getting this:
"html_attributions" : [],
"results" : [
"icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/shopping-71.png",
"id" : "b89943f74e20eafb8959ace996a6f35cd303d5ff",
"name" : "Circle K",
"opening_hours" : {
"open_now" : true,
"weekday_text" : []
},
"photos" : [
{
"height" : 400,
"html_attributions" : [
"\u003ca href=\"https://maps.google.com/maps/contrib/114668422179892290420/photos\"\u003eCircle K\u003c/a\u003e"
],
"photo_reference" : "CmRaAAAAAdubSCt5u9IrI6bsL91DGLdIm2SMO39SaKGxKKmrj-ygxrCmIjUzG0DwrBgcX56DdpEFRBev29cdA4ljZnIR7lvPlP3jBhHkBxliRkGlAlfkAh9TiQvVuyNNQLnzIcsuEhAsc0Px0hV4mFTZruHRWLh9GhTIePuzk5JxV1QewH3jimkAKIzALw",
"width" : 400
}
],
Instead, I'm getting:
"html_attributions" : [],
"results" : [
"icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png",
"id" : "1a830f99aab4e7afa143b2c8d03545ea7c1e9432",
"name" : "McDonald's",
"opening_hours" : {
"open_now" : true,
"weekday_text" : []
},
The "photos" part of the JSON is being left on some calls, any ideas? Thanks.
According to Google Place Search documentation (which is the service you're using, I guess), each result object may contain:
photos[] — an array of photo objects, each containing a reference to an image. A Place Search will return at most one photo object.
Also, Google Place Photos documentation reports that:
The response [...] will contain a photos[] field if the place has related photographic content.
That means that in case a specific result has no photos, then photos array won't be present at all in the response.
The examples you provided are not referring to the same place: in the first case you're getting a result for "Circle K", while in the second case for "McDonald's". Based on that, the response seems coherent with what the documentation says.

How to export/save DataSnapshot Firebase in Json file?

I have very big problem. I have this structure in my Firebase:
I would like to save this structure in JSON file/String no matter. I would get such a result:
{
"-Ka8_NYNxaFCcZQ3eaTp" : {
"Persons" : {
"-KaHUTUan-l06OVjsUlu" : {
"birthDay" : "13.01.2017",
"deathDay" : "06.01.2017",
"lastName" : "LastName1",
"latitude" : "54.2039383",
"longitude" : "16.1930868",
"name" : "name1"
},
"-KaHU_nPOE2NlqTbhLxT" : {
"birthDay" : "11.01.2017",
"deathDay" : "03.01.2017",
"lastName" : "LastName2",
"latitude" : "54.2039383",
"longitude" : "16.1930868",
"name" : "name2"
}
},
"city" : "Wroclaw",
"county" : "county1",
"nameCemetery" : "nameCemetery1",
"province" : "province1",
"street" : "street1"
}
I tried through DataSnaphot but the result was not in the format JSON, e.g:
{-Ka8_NYNxaFCcZQ3eaTp={nameCemetery=nameCemetery1, street=street1, county=county1, province=province1, Persons={-KaHU_nPOE2NlqTbhLxT={name=Damian, latitude=54.2039383, lastName=Szeeczyk, birthDay=11.01.2017, longitude=16.1930868, deathDay=03.01.2017}, -KaHUTUan-l06OVjsUlu={name=name1, latitude=54.2039383, lastName=LastName1, birthDay=13.01.2017, longitude=16.1930868, deathDay=06.01.2017}}, city=Wroclaw}}
The most important for me is not to create JSONArray Persons, because then my application will run incorrectly.
In short, I would like to make a copy node (in my example key -"Ka8_NYNxaFCcZQ3eaTp"

Querying firebase with filtering data

So I have the Json in following format on firebase:
{
"30000000549656652" : {
"cast" : [ {
"src" : "https://encrypted-tbn1.gstatic.com/images",
"title" : "Hilary Duff (Samantha \"Sam\" Montgomery)"
}],
"channel_name" : "HBO",
"desc" : "After her .....",
"extras" : {
"initial" : "July 10, 2004 (Hollywood)",
"director" : "Mark Rosman",
"screenplay" : "Leigh Dunlap"
},
"genre" : "Thriller",
"lang" : "English",
"movie" : 1,
"prog_id" : 30000000549656652,
"prog_img" : "http://images."
"ratings" : {
"IMDb" : "5.9/10",
"Metacritic" : "25%",
"Rotten Tomatoes" : "11%"
},
"start" : 201607141800,
"stop" : 201607141700,
"title" : "Thrill A Cinderella Story",
"year" : "2004"
},
.....
}
I want to do the following in activity on my android app-
Query the node in which these JSON exist, then filter them such that i get all the results which have movies of one specific genre(e.g., Thriller) and the year is within five yrs less or five yrs more of the one i would pass in the query.
Like i would want to query to get all thriller movies whose "year" are between 2000 and 2009.
Can someone please help me to build such query for a json like this. Thanks!

Snakecase in a retrofit call with gson

I am using the original retrofit to call the Instagram api. The call returns this reponse
{ "data" : [ { "attribution" : null,
"caption" : { "created_time" : "1460953831",
"from" : { "full_name" : "Edward ♕",
"id" : "282525924",
"profile_picture" : "https://scontent.cdninstagram.com/t51.2885-19/s150x150/12519400_165282517199271_1363863979_a.jpg",
"username" : "manlike_eddy"
},
"id" : "17847512245075242",
"text" : "Oh girl"
},
"comments" : { "count" : 1,
"data" : [ { "created_time" : "1460953885",
"from" : { "full_name" : "Edward ♕",
"id" : "282525924",
"profile_picture" : "https://scontent.cdninstagram.com/t51.2885-19/s150x150/12519400_165282517199271_1363863979_a.jpg",
"username" : "manlike_eddy"
},
"id" : "17847512257075242",
"text" : "#love #TagsForLikes #TagsForLikesApp #instagood #me #smile #follow #cute #photooftheday #tbt #followme #girl #beautiful #happy #picoftheday #instadaily #food #swag #amazing #TFLers #fashion #igers #fun #summer #instalike #bestoftheday #smile #like4like #friends #instamood"
} ]
},
"created_time" : "1460953831",
"filter" : "Normal",
"id" : "1230892414266144399_282525924",
"images" : { "low_resolution" : { "height" : 320,
"url" : "https://scontent.cdninstagram.com/t51.2885-15/s320x320/e35/12965764_932183320233898_1928407701_n.jpg?ig_cache_key=MTIzMDg5MjQxNDI2NjE0NDM5OQ%3D%3D.2.l",
"width" : 320
},
"standard_resolution" : { "height" : 640,
"url" : "https://scontent.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/12965764_932183320233898_1928407701_n.jpg?ig_cache_key=MTIzMDg5MjQxNDI2NjE0NDM5OQ%3D%3D.2.l",
"width" : 640
},
"thumbnail" : { "height" : 150,
"url" : "https://scontent.cdninstagram.com/t51.2885-15/s150x150/e35/c257.0.565.565/12950340_1566398773652655_1170974002_n.jpg?ig_cache_key=MTIzMDg5MjQxNDI2NjE0NDM5OQ%3D%3D.2.c",
"width" : 150
}
},
"likes" : { "count" : 0,
"data" : [ ]
},
"link" : "https://www.instagram.com/p/BEVAiEVDnaP/",
"location" : null,
"tags" : [ "beautiful",
"cute",
"fashion",
"love",
"summer",
"food",
"instalike",
"tbt",
"tagsforlikesapp",
"igers",
"follow",
"instadaily",
"instamood",
"friends",
"girl",
"me",
"swag",
"like4like",
"tflers",
"followme",
"instagood",
"tagsforlikes",
"amazing",
"bestoftheday",
"fun",
"smile",
"photooftheday",
"picoftheday",
"happy"
],
"type" : "image",
"user" : { "full_name" : "Edward ♕",
"id" : "282525924",
"profile_picture" : "https://scontent.cdninstagram.com/t51.2885-19/s150x150/12519400_165282517199271_1363863979_a.jpg",
"username" : "manlike_eddy"
},
"user_has_liked" : false,
"users_in_photo" : [ ]
} ],
}
}
As you can see some of the labels are using snakecase, such as standard_resolution. When I plug this data into Json Pogo it creates two classes for standard_resolution. One named StandardResolution and one StandardResolution. When I call .getStandardResolution() on the respective object null is returned, even though it should have valid data.

Categories

Resources