Getting keys from JSON using volley [duplicate] - android

This question already has answers here:
JSON Parsing in Android [duplicate]
(5 answers)
Closed 6 years ago.
{
"ResponseType": "success",
"ResponseMessage": "success",
"Personal Preference": [
{
"Questionno": 3,
"TokenKey": "150920150252340989472d8d09fc6563",
"QuestionText": "Km per jaar",
"QuestionType": "Slider",
"QuestionOrder": 3,
"answerOptionList": [],
"Answer": "16014",
"Unit": "KM",
"Minimum": 1,
"Maximum": 100000,
"SliderOptionList": [
"<1000",
"1000-1500",
"1501-2000",
"2001-3000",
"3001-5000",
">5000"
]
The above is the JSON which i receive from the server using Volley. I need to get only the Keys(ResponseType, ResponseMessage, Personal Preference: these might varry) from this JSON and store in an array.

Use this code
Iterator iterator = jsonObject.keys();
List<String> keysList = new ArrayList<String>();
while(iterator.hasNext()) {
String key = (String) iterator.next();
keysList.add(key);
}

Related

How to parse this json array [duplicate]

This question already has answers here:
How do I parse JSON in Android? [duplicate]
(3 answers)
Closed 5 years ago.
How to fetch Keys inside date arrays i am able to parse till '2'how to get that 19:00:00
"date_times": {
"2": {
"2018-01-08": [
**"19:00:00"**
],
"2018-01-09": [
"13:30:00",
"19:00:00"
],
"2018-01-10": [
"13:30:00",
"19:00:00"
],
"2018-01-11": [
"13:30:00",
"19:00:00"
]
}
}
If you concern about getting keys from jsonobject . Then just use an Iterator on keys.
try {
JSONObject resObject = new JSONObject(response);
Iterator<String> iterator = resObject.keys();
while (iterator.hasNext()) {
String key = iterator.next();// this will be your date
JSONArray data = resObject.getJSONArray(key);// And this is your data
}
} catch (Exception e) {
}
This is just an example modify it as per your need

android json starting with string [duplicate]

This question already has answers here:
How do I parse JSON in Android? [duplicate]
(3 answers)
Closed 6 years ago.
returnLogin([{
"id": 0,
"first_name": "abc",
"last_name": "xyz",
"msg": "Invalid User or Location ID or Password.",
"location_id": "",
}])
How to get data from above Json string?
here json start with witn string not any bracket thats why i try to. getting data
you can parse like below,
String data = "";// here your json data which will parse
try {
JSONArray jsonArray = new JSONArray(data);
JSONObject jsonObject=jsonArray.getJSONObject(0);
int id=jsonObject.getInt("id");
String first_name=jsonObject.getString("first_name");
String last_name=jsonObject.getString("last_name");
String msg=jsonObject.getString("msg");
String location_id=jsonObject.getString("location_id");
} catch (JSONException e) {
e.printStackTrace();
}
But before parse correct your data.

Android parse JSON with multiple array response from php [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I recently started working with andorid.
How to parse dynamic json with multiple arrays response from php server.
Login Response
{
"status": 1,
"data": {
"msg": "LoggedIn",
"user_id": "2"
}
}
Login response with error message
{
"status": 0,
"data": "No users found with given email."
}
and an other one
Inventory List
{
"status": 1,
"data": [
{
"inventory_id": "33",
"apron_id": "123456",
"nickname": "uyi",
"location": "13",
"manufacture": "0",
"garment_type": "yuyh",
"color": "juki",
"core_material": "ytyt",
"Date_purchase": "2015-04-10",
"UID_no": "ikujki",
"serial": "iui",
"Batch": "ikk",
"Expiration": "2015-04-23",
"QTY": "898",
"apron_status": "0",
"apron_retire": "0",
"created_user": "2",
"created_time": "2015-04-10 05:22:38",
"update_time": "2015-04-10 05:22:38"
},
{
"inventory_id": "32",
"apron_id": "12345mn",
"nickname": "gfhgh",
"location": "12",
"manufacture": "0",
"garment_type": "hgjyhj",
"color": "ytgtfghtg",
"core_material": "fhgfhy",
"Date_purchase": "2015-04-28",
"UID_no": "rtryttttttttt",
"serial": "hfh",
"Batch": "rtrrtyy",
"Expiration": "2015-03-17",
"QTY": "7688",
"apron_status": "0",
"apron_retire": "0",
"created_user": "2",
"created_time": "2015-04-10 05:15:54",
"update_time": "2015-04-10 05:15:54"
}
]
}
Thanks in advance.
to get login response you can do like this
JSONObject jobj=new JSONObject(result.toString());
String status=jobj.getString("status");
if(status.equalsIgnoreCase("1"))
{
//login success
JSONObject Jdata=jobj.getJSONObject("data");
String Message=Jdata.getString("msg");
String UserId=Jdata.getString("user_id");
}
else
{
//failure
}
and for Inventory List you can do like this
JSONObject jobj=new JSONObject(result.toString());
JSONArray arrData=jobj.getJSONArray("data");
for (int i = 0; i < arrData.length(); i++)
{
JSONObject jdata=arrData.getJSONObject(i);
//here u can get all field like this
String nickname=jdata.getString("nickname");
}
Investigate the JSONObject and JSONArray classes.
You can parse extremely simply:
JSONObject json = new JSONObject(jsonString);
int status = json.getInt("status");
Using both an Array and an Object for your "data" key will make things irritating for you. You should think about using a different key, or another key that will dictate to you what you are reading.

How to read Json data in android [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a raw data as in a below format:
[
{
"id": "1",
"name": "abc",
"type": "consumer"
},
{
"id": "2",
"name": "cdf",
"type": "consumer"
},
{
"id": "3",
"name": "jok",
"type": "owner"
}
]
Please let me know how can I covert that into JsonArray and get the each values.
This is the simplest way to parse your JSON String. I would suggest your to read JSON documents.
But, here is a sample code.
try {
String jsonString = new String("[{\"id\": \"1\",\"name\": \"abc\",\"type\": \"consumer\"}]");
JSONArray jsonArray = new JSONArray(jsonString);
for(int index = 0;index < jsonArray.length(); index++) {
JSONObject jsonObject = jsonArray.getJSONObject(index);
String id = jsonObject.getString("id");
String name = jsonObject.getString("name");
String type = jsonObject.getString("type");
}
} catch (JSONException e) {
e.printStackTrace();
}
You can also parse using GSON https://code.google.com/p/google-gson/

How to parse json string in Android? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
JSON Array iteration in Android/Java
I am fetching JSON string from server and I have already got JSON string by code.
But I didn't understand how to parse it.
Below is my JSON string
{
"university": {
"name": "oxford",
"url": "http://www.youtube.com"
},
"1": {
"id": "2",
"title": "Baseball",
"datetime": "2011-11-11 10:41:46"
},
"2": {
"id": "1",
"title": "Two basketball team players earn all state honors",
"datetime": "2011-11-11 10:40:57"
}
}
Please provide any guidance or code snippet.
Use JSON classes for parsing e.g
JSONObject mainObject = new JSONObject(Your_Sring_data);
JSONObject uniObject = mainObject.getJSONObject("university");
String uniName = uniObject.getString("name");
String uniURL = uniObject.getString("url");
JSONObject oneObject = mainObject.getJSONObject("1");
String id = oneObject.getString("id");
....
Below is the link which guide in parsing JSON string in android.
http://www.ibm.com/developerworks/xml/library/x-andbene1/?S_TACT=105AGY82&S_CMP=MAVE
Also according to your json string code snippet must be something like this:-
JSONObject mainObject = new JSONObject(yourstring);
JSONObject universityObject = mainObject.getJSONObject("university");
JSONString name = universityObject.getString("name");
JSONString url = universityObject.getString("url");
Following is the API reference for JSOnObject: https://developer.android.com/reference/org/json/JSONObject.html#getString(java.lang.String)
Same for other object.

Categories

Resources