This question already has answers here:
Sending and Parsing JSON Objects in Android [closed]
(11 answers)
Closed 7 years ago.
I have a json+collection string as following:
//code formated
{ "collection" :
{
"version" : "1.0",
"href" : "http://example.org/friends/",
"links" : [
{"rel" : "feed", "href" : "http://example.org/friends/rss"}
],
"items" : [
{
"href" : "http://example.org/friends/jdoe",
"data" : [
{"name" : "full-name", "value" : "J. Doe", "prompt" : "Full Name"},
{"name" : "email", "value" : "jdoe#example.org", "prompt" : "Email"}
],
"links" : [
{"rel" : "blog", "href" : "http://examples.org/blogs/jdoe", "prompt" : "Blog"},
{"rel" : "avatar", "href" : "http://examples.org/images/jdoe", "prompt" : "Avatar", "render" : "image"}
]
},
{
"href" : "http://example.org/friends/msmith",
"data" : [
{"name" : "full-name", "value" : "M. Smith", "prompt" : "Full Name"},
{"name" : "email", "value" : "msmith#example.org", "prompt" : "Email"}
],
"links" : [
{"rel" : "blog", "href" : "http://examples.org/blogs/msmith", "prompt" : "Blog"},
{"rel" : "avatar", "href" : "http://examples.org/images/msmith", "prompt" : "Avatar", "render" : "image"}
]
},
{
"href" : "http://example.org/friends/rwilliams",
"data" : [
{"name" : "full-name", "value" : "R. Williams", "prompt" : "Full Name"},
{"name" : "email", "value" : "rwilliams#example.org", "prompt" : "Email"}
],
"links" : [
{"rel" : "blog", "href" : "http://examples.org/blogs/rwilliams", "prompt" : "Blog"},
{"rel" : "avatar", "href" : "http://examples.org/images/rwilliams", "prompt" : "Avatar", "render" : "image"}
]
}
],
"queries" : [
{"rel" : "search", "href" : "http://example.org/friends/search", "prompt" : "Search",
"data" : [
{"name" : "search", "value" : ""}
]
}
],
"template" : {
"data" : [
{"name" : "full-name", "value" : "", "prompt" : "Full Name"},
{"name" : "email", "value" : "", "prompt" : "Email"},
{"name" : "blog", "value" : "", "prompt" : "Blog"},
{"name" : "avatar", "value" : "", "prompt" : "Avatar"}
]
}
}
}
sample linK: http://amundsen.com/media-types/collection/examples/
Can you please help me on how to decode this message. I have to read items , queries into my code and also version etc. I have to do this on android. I am not able to get how to use JSONObject to fetch the items, queries into my code.
For the array objects use JSONArray. Method arr.length() returns elements count, arr.get<type>(position) or just arr.get(position) for getting element.
you may use:
import org.json.JSONException;
import org.json.JSONObject;
in the following way:
try {
JSONObject myObj = new JSONObject("your json string");
} catch (JSONException e) {
e.printStackTrace();
}
Related
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?
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.
Okay, so I have my distance matrix request responding as if I didn't pass it an arrival time.
https://maps.googleapis.com/maps/api/distancematrix/json?origins=875+Centerville+Road+Rhode+Island&destinations=1+New+England+Tech+Blvd&key=AIzaSyAkoZCNoX406xz-mmv2_O9Hji7oAkcoN7g&transit_mode=bus&arrival_time=194976000&sensor=false
This is the response I get back even with the arrival time parameter specified...
{
"destination_addresses" : [ "1 New England Tech Boulevard, East Greenwich, RI 02818, USA" ],
"origin_addresses" : [ "875 Centerville Rd, Warwick, RI 02886, USA" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "5.0 km",
"value" : 5048
},
"duration" : {
"text" : "8 mins",
"value" : 488
},
"status" : "OK"
}
]
}
], "status" : "OK"
}
I get the same response if the url I pass is as follows...
https://maps.googleapis.com/maps/api/distancematrix/json?origins=875+Centerville+Road+Rhode+Island&destinations=1+New+England+Tech+Blvd&key=AIzaSyAkoZCNoX406xz-mmv2_O9Hji7oAkcoN7g
I get the same result whether or not I pass the arrival time...
{
"destination_addresses" : [ "1 New England Tech Boulevard, East Greenwich, RI 02818, USA" ],
"origin_addresses" : [ "875 Centerville Rd, Warwick, RI 02886, USA" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "5.0 km",
"value" : 5048
},
"duration" : {
"text" : "8 mins",
"value" : 488
},
"status" : "OK"
}
]
}
], "status" : "OK"
}
Can someone please tell me what I'm doing wrong. I need the API to respond with what time I will need to leave, to arrive at a specified time that I pass it.
I've not found a working example and this part is crucial to my application. Thank you.
I have a working response for the departure time parameter...but doing the same with the arrival time doesn't work. Does this API not actually work for the arrival time?
https://maps.googleapis.com/maps/api/distancematrix/json?origins=875+centerville+road+Rhode+Island&destinations=1+new+england+tech+blvd+rhode+island&key=AIzaSyAkoZCNoX406xz-mmv2_O9Hji7oAkcoN7g&transit_mode=bus&departure_time=1949794160
{
"destination_addresses" : [ "1 New England Tech Boulevard, East Greenwich, RI 02818, USA" ],
"origin_addresses" : [ "875 Centerville Rd, Warwick, RI 02886, USA" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "5.0 km",
"value" : 5048
},
"duration" : {
"text" : "8 mins",
"value" : 488
},
"duration_in_traffic" : {
"text" : "7 mins",
"value" : 432
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}
I'm a beginner in android development ... i want to know how to create a json structure, with using arraylists and hashmaps ... you can find the structure as follows ...
NOTE: I don't want to use JSONArray or JSONObject
[
[
{
"blog_owner" : "user",
"icalIssueEventID" : "",
"updated_date" : "2014-02-17 07:05:53 +0000",
"icalEventID" : "",
"blog_stage" : "Cancelled",
"blog_ID" : "GMpk20140206160708281",
"created_date" : "2014-02-06 10:37:38 +0000",
"emailFlag" : false
}
],
[
{
"blog_iNotes_ID" : "",
"blog_name" : "",
"blog_status" : "",
"blog_duration" : ""
},
{
"pid" : "0",
"isOpen" : true,
"id" : "R1",
"level" : 0,
"value" : [
{
"level" : 0,
"id" : "MA20140206181038936",
"type" : "meetingAgenda",
"isOpen" : true,
"value" : [
{
"pid" : "MA20140206181038936",
"level" : 1,
"id" : "CN20140207211127374",
"isOpen" : false,
"value" : [
],
"line_number" : 13,
"keyItem" : "Fgh",
"isCategorySelected" : false
},
{
"line_number" : 14,
"level" : 1,
"id" : "CN20140217123343769",
"isOpen" : true,
"value" : [
],
"keyItem" : "Fghh",
"isCategorySelected" : false,
"pid" : "MA20140206181038936"
},
{
"pid" : "MA20140206181038936",
"level" : 1,
"id" : "ISS20140217123542229",
"isOpen" : true,
"type" : "Issues",
"value" : [
],
"type_KeyItem" : {
"tfImpact" : "",
"tvIssueDescription" : "",
"tfActionRequired" : "Ffg",
"tfResponsible" : "",
"targetDate" : "Month DD YYYY"
},
"keyItem" : "Issues",
"isCategorySelected" : true,
"line_number" : 15
}
],
"type_KeyItem" : {
"tfAgendaTitle" : "New agenda for magic minutes",
"lblDuration" : "00:00",
"tfAgendaName" : ""
},
"line_number" : 11,
"isCategorySelected" : false,
"pid" : "R1"
}
],
"isCategorySelected" : false,
"keyItem" : "root",
"line_number" : 10
}
],
[
]
]
You can use google-gson library for converting JSON to Java objects and vice-versa.
Some other helpful links are :
1) http://www.javacreed.com/simple-gson-example/
2) http://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/
Hope this will help you.
I saw in the official documentation of google places : https://developers.google.com/places/documentation/search
that the response gave from the server to search is :
"results" : [
{
"formatted_address" : "529 Kent Street, Sydney NSW, Australia",
"geometry" : {
"location" : {
"lat" : -33.8750460,
"lng" : 151.2052720
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png",
"id" : "827f1ac561d72ec25897df088199315f7cbbc8ed",
"name" : "Tetsuya's",
"rating" : 4.30,
"reference" : "CnRmAAAAmmm3dlSVT3E7rIvwQ0lHBA4sayvxWEc4nZaXSSjRtfKRGoYnfr3d5AvQGk4e0u3oOErXsIJwtd3Wck1Onyw6pCzr8swW4E7dZ6wP4dV6AsXPvodwdVyqHgyGE_K8DqSp5McW_nFcci_-1jXb5Phv-RIQTzv5BjIGS0ufgTslfC6dqBoU7tw8NKUDHg28bPJlL0vGVWVgbTg",
"types" : [ "restaurant", "food", "establishment" ]
}...
I would like to know if it is posible to reach info like comments,
basically to reach more info that those 8 variables.
Tx !
it is disponible en :
"results" : [
"reviews" : [
{
"aspects" : [
{
"rating" : 3,
"type" : "quality"
}
],
"author_name" : "Simon Bengtsson",
"author_url" : "https://plus.google.com/104675092887960962573",
"text" : "Just went inside to have a look at Google. Amazing.",
"time" : 1338440552869
},
{
"aspects" : [
{
"rating" : 3,
"type" : "quality"
}
],
"author_name" : "Felix Rauch Valenti",
"author_url" : "https://plus.google.com/103291556674373289857",
"text" : "Best place to work :-)",
"time" : 1338411244325
},
{
"aspects" : [
{
"rating" : 3,
"type" : "quality"
}
],
"author_name" : "Chris",
"text" : "Great place to work, always lots of free food!",
"time" : 1330467089039
}
],
"types" : [ "establishment" ],
"url" : "http://maps.google.com/maps/place?cid=10281119596374313554",
"vicinity" : "48 Pirrama Road, Pyrmont",
"website" : "http://www.google.com.au/"
},
Official page: https://developers.google.com/places/documentation/details