Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Now, I want to create Gridview with section and I do following
this
and then I stuck problem at I don't know how to get position section and item from JSONArray
I have JSONArray like this
“Team”: [
{
"team_id": "16",
"team_name": "3",
"team_max_player": "6",
"team_amount": "2",
"team_member_list": [
{
"id": "19",
"room_id": "23",
"user_id": "75",
"team_id": "16",
"detail_status": ""
},
{
"id": "21",
"room_id": "23",
"user_id": "46",
"team_id": "16",
"detail_status": ""
}
]
},
{
"team_id": "14",
"team_name": "1",
"team_max_player": "1",
"team_amount": "1",
"team_member_list": [
{
"id": "20",
"room_id": "23",
"user_id": "40",
"team_id": "14",
"detail_status": ""
}
]
}
]
I want to set "team_id" as section and "team_member_list" as an item in that section.
please teach me to do it.
I am currently in grade 12 in school with programing class, android studio, JAVA Moblie App. Please help me and sorry about my English.
First you can load the JSON as a string and parse the JSON string using the JSONObject class.
You can then use the JSONArray class to parse the JSON Array inside your JSONObject to retrieve the details you want.
See the code sample below that will log each team member's id along with the id of which team they belong to in the console.
JSONObject jsonObject = new JSONObject(jsonString);
JSONArray teams = jsonObject.getJSONArray("Team");
for (int i = 0; i < teams.length(); i++) {
String teamId = jsonObject.getJSONArray("Team").getJSONObject(i).getString("team_id");
JSONArray teamMemberList = jsonObject.getJSONArray("Team").getJSONObject(i).getJSONArray("team_member_list");
for (int j = 0; j < teamMemberList.length(); j++) {
String teamMemberId = teamMemberList.getJSONObject(j).getString("id");
Log.i("team", "team id: " + teamId + " team member id: " + teamMemberId);
}
}
If you would like to learn more about JSON parsing in Android, I wrote a JSON tutorial for Android that goes into much greater detail.
Related
I'm trying to have a list of objects from a server. But I don't know how to do it exactly and at least print it in System.out.print
server sends data like that:
[
{
"userId": "15",
"routeId": "10",
"driverId": "2",
"text": "ytf",
"timestamp": "2018-05-25 13:04:01"
},
{
"userId": "15",
"routeId": "33",
"driverId": "2",
"text": "asd",
"timestamp": "2018-05-25 13:07:40"
}
]
And model:
https://pastebin.com/52mc5Gmw
https://pastebin.com/DnUTtkPC
Controller: https://pastebin.com/ucJFFJyy
And the API:
#GET("getReviews.php")
Call<List<reviewResult>> getReview();
You can use JSONArray for parse the array, and JSONObject for get each object of the array
Edit Add a example
JSONArray jsonArray = new JSONArray(response);
for(int i=0; i<jsonArray.length();i++)
{
JSONObject object = jsonArray.getJSONObject(i);
System.out.print(object);
}
From your code, looks like the expected return type should be List of reviewResponse objects. You should change the API to:
Call<List<reviewResponse>> getReview();
From there it's obvious how to retrieve the data you ask
This question already has answers here:
How to parse JSON in Java
(36 answers)
Closed 5 years ago.
Here's the JSON:
{
"firstName": "John",
"lastName": "doe",
"age": 26,
"address": {
"streetAddress": "123 Main street",
"city": "Anytown",
"postalCode": "12345"
},
"phoneNumbers": [
{
"type": "iPhone",
"number": "123-456-8888"
},
{
"type": "home",
"number": "123-557-8910"
}
]
}
The question is, in Java, how do you access the address fields? I tried things like address.city, but that didn't work:
String city = (String) jsonObject.get("address.streetAddress");
System.out.println(city);
Would appreciate any suggestions.
You need to call getString on the address object that you get.
String city = jsonObject.getJSONObject("address").getString("streetAddress");
{
"firstName": "John",
"lastName": "doe",
"age": 26,
"address": {
"streetAddress": "123 Main street",
"city": "Anytown",
"postalCode": "12345"
},
"phoneNumbers": [
{
"type": "iPhone",
"number": "123-456-8888"
},
{
"type": "home",
"number": "123-557-8910"
}
]
}
first get your address.Address is an object.{} means object,[] means an array.
JSONObject addressObj = jsonObject.get("address");
now you have a reference to the address.
String addressStr = addressObj.get("streetAddress");
String cityStr = addressObj.get("city");
int cityInt = Integer.parseInt(addressObj.get("postalCode"));
If i don't wrong remember there should be getString("streetAddress") and getInteger("postalCode") to avoid parse issues.
Apart from the given answers you should add a check to see if the address is not blank before checking city or else you will end up getting and exception.
Here is updated example:
if( jsonObject.has("address"){
JSONObject addressObj = jsonObject.get("address");
if( addressObj.has("city"){
String cityStr = addressObj.get("city");
}
}
I'm downloading a JSON file with the structure:
{
"options": {
"toolbar": "1",
"full": "1",
"exit": "0",
"about": "0"
},
"general": {
"versioncontrol": "1.0"
},
"companydetails": {
"toolbaricons": {
"1": [
"Facebook",
"xxx",
"xxx"
],
"2": [
"Instagram",
"xxx",
"xxx"
],
"3": [
"LinkedIn",
"xxx",
"xxx"
]
},
"buttons": "4",
"openingtimes": {
"1": "10:00 - 16:00",
"2": "9:00 - 16:00"
}
}
}
I've managed to extract it from the server as a string which I am now storing in a variable.
I've tried
JSONObject jsonObj = new JSONObject(fileOutput);
JSONArray details = jsonObj.getJSONArray("options");
JSONObject c = details.getJSONObject(0);
But I cannot seem to extract anything from it. I want to be able to access all different bits in options etc and gather the information. I've tried to print to log the details of options etc, but I can't seem to get it to work. I had it working before but now the file structure has been updated and the old method I was using no longer works.
Options is not array but object, use:
JSONObject jsonObj = new JSONObject(fileOutput);
JSONObject options = jsonObj.getJSONObject("options");
int toolbar = options.getInt("toolbar");
I am fetching details from a database table which contains 3 rows in JAVA.
I am using JSONarray and JSONObject as follows
JSONObject jsonObject = new JSONObject();
JSONObject mainjsonObject = new JSONObject();
JSONArray ja=new JSONArray();
The data from table is put to the jsonObject as follows for each one:
String qry="select * from details";
ResultSet res = select .executeQuery(qry);
while(res.next){
String Name=res.getString("name");
String age=res.getString("age");
.
.
jsonObject.put("Name", Name);
jsonObject.put("age", age);
.
.
ja.put(jsonObject);
}
mainjsonObject.put("PERSONAL DETAILS",ja);
I should get the output json as follows(i.e. the order in which i entered):
{
"PERSONAL DETAILS": [
{
" name": "abc",
"age": "4",
"gender": "F",
"Place": "abc1"
},
{
" name": "xyz",
"age": "3",
"gender": "M",
"Place": "abc2"
}
]
}
But am getting the values in random order like below:
{
"PERSONAL DETAILS": [
{
"age": "4",
" name": "abc",
"Place": "abc1"
"gender": "F",
},
{
"age": "3",
" name": "xyz",
"Place": "abc2"
"gender": "M",
}
]
}
Please help me with a solution. I need to get all the values in the same order in which i have entered.
You can try something like this to build json object construct using LinkedHashMap and then pass it to the constructor like this ,
LinkedHashMap<String, String> jsonOrderedMap = new LinkedHashMap<String, String>();
jsonOrderedMap.put("Name", res.getString(1));
...
...
// struct you want
JSONObject JSONorder = new JSONObject(jsonOrderedMap);
JSONArray sortedJSON = new JSONArray(Arrays.asList(JSONorder));
I am building an Android application which reads from themoviedb.org.
What I am trying to do is have the user enter a movie title and use that title to find its id.
When I run the query to search for movies, I get a response like:
{
"page": 1,
"results": [
{
"poster_path": "aaaaa.jpg",
"id": "11",
"description": "MovieDescription"
},
{
"poster_path": "bbbbb.jpg",
"id": "12",
"description": "MovieDescription2"
},
{
"poster_path": "ccccc.jpg",
"id": "13",
"description": "MovieDescription"
}
]
}
Using the Maven JSON library, I can fetch the results key as a string using json.get("results").
returning:
[
{
"poster_path": "aaaaa.jpg",
"id": "11",
"description": "MovieDescription"
},
{
"poster_path": "bbbbb.jpg",
"id": "12",
"description": "MovieDescription2"
},
{
"poster_path": "ccccc.jpg",
"id": "13",
"description": "MovieDescription"
}
]
But I want to convert the first of these results to another JSONObject so that I can get the movie's id from the first result.
I'm thinking that the way to do this is to convert the results value to a list of JSONObject and then use the json.get("id") method on the first object in the list. But I do not know how to do this conversion.
Any help would be appreciated.
You can use JSONObject.getJSONArray to get the result directly as a JSON Array:
JSONArray results = json.getJSONArray("results") // Get results as JSON Array
JSONObject first = results.getJSONObject(0) // Get first object as JSON Object
See: JSONObject#getJSONArray(String)