How to parse this json array [duplicate] - android

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

Related

How to parse JSON array in android?[JSON structure below in description] [duplicate]

This question already has answers here:
How to parse JSON in Java
(36 answers)
Closed 6 years ago.
I have an Json Array Like -
[
500,
"Applied Before"
]
How can I parse it in android.
You can try like this
String data = " [500," +
" \"Applied Before\"] ";
try {
JSONArray arr = new JSONArray(data);
Log.i("arr",""+arr.get(0));
} catch (JSONException e) {
e.printStackTrace();
}
Improper JSON Array syntax .
Example of a JSON Array :
"employees":[
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter","lastName":"Jones"}
]

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.

Getting keys from JSON using volley [duplicate]

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);
}

Parsing multiple json arrays in a json object

I have a json object being returned that has multiple arrays within the json object. I am able to parse a json object if it has one array in it (with jObject.getJSONArray("2013-10-30")), but not multiple. The amount of arrays (i.e. the dates) would be changing almost everyday (some days there wouldn't be any) and that is where I get stuck. I'm not sure how to get a list of all the array names and then iterator through that.
{
"2013-10-30": [
{
"id": "399",
"Time": "00:50:46"
}
],
"2013-10-29": [
{
"id": "398",
"Time": "21:44:09"
},
{
"id": "393",
"Time": "10:53:01"
}
]
}
You should iterate over the keys and get the JSONArray for each key as follow:
Iterator<String> keys = jObject.keys();
while(keys.hasNext()){
String key = keys.next();
try{
JSONArray array = jObject.getJSONArray(key);
// do something with the array
// ...
}
catch(Exception e){
e.printStackTrace()
}
}

how to access the json array in android [duplicate]

This question already has answers here:
How to call the JSON object in Android
(5 answers)
Closed 8 years ago.
i am trying to use one json file in my project.i have one Json file in following structure,i succeed to access json array "business", but now i want to access business_cat and in business_cat, cat 1 and cat 2. how can i access these values?
{
"business":[
{
"id":"13",
"category":"Dinner",
"subcategory":"",
"name_eng":"dinner 1",
"name_arab":"dinner 1",
"mobile":"12345",
"address":"not now",
"logo":"1.gif",
"contact":"Call",
"open_time":" ",
"close_time":" "
}
],
"business_cat":[
[
"cat 1",
{
"name":"dish 1",
"id":"7",
"name_arab":"dish1",
"price":"200",
"logo":"laptop.jpeg"
},
{
"name":"dish 2",
"id":"8",
"name_arab":"dish 2",
"price":"123789",
"logo":"micky.jpg"
},
{
"name":"qaz",
"id":"10",
"name_arab":"zaq",
"price":"12",
"logo":"watch.jpg"
},
{
"name":"wsx",
"id":"11",
"name_arab":"xsw",
"price":"12",
"logo":"micky.jpg"
},
{
"name":"zxc",
"id":"12",
"name_arab":"vcxz",
"price":"34",
"logo":"camera.jpg"
}
],
[
"cat2",
{
"name":"d1",
"id":"9",
"name_arab":"d1",
"price":"300",
"logo":"watch.jpg"
}
]
]
}
A JSON Object starts with a { and ends with a } while a JSON Array starts with a [ and ends with a ].
The whole string will be JSONObject, from which each array can be extracted via json.getJSONArray(ARRAYNAME)
JSONObject json = new JSONObject(jsonString);
JSONArray business = json.getJSONArray("business");
//business
System.out.println("*****business*****"+business.length());
for(int i=0;i<business.length();i++){
JSONObject json_data_business = business.getJSONObject(i);
Log.i("log","id"+json_data_business.getInt("id")+
", category"+json_data_business.getString("category")
);
// business_cat
JSONArray business_cat = json.getJSONArray("business_cat");
System.out.println("*****business_cat*****"+business_cat.length());
for(int i=0;i<business_cat.length();i++){
JSONObject json_data_business_cat = business_cat.getJSONObject(i);
Log.i("log","id"+json_data_business_cat.getInt("id")+
", name"+json_data_business_cat.getString("name")
);
}
JSONObject obj = new JSONObject(content);
if (obj != null) {
Iterator<?> it = obj.keys();
while (it.hasNext()) {
String key = it.next().toString();
JSONArray value = (JSONArray) obj.optString(key);
---then you parse the JSONArray
}
}

Categories

Resources