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 7 years ago.
Improve this question
How i can parse 2 json Array in Android?
Plz see the following code;
{
"detail": [
{
"price": 51,
"numsc": 2,
"name": "this app is about animals",
"sc1": "printed-dress.jpg",
"sc2": "printed-dress2.jpg"
}
],
"colors": [
{
"color": "#5D9CEC",
"name": "blue"
},
{
"color": "#FCCACD",
"name": "pink"
}
]
}
can you help me plz??
JSONObject object = new JSONObject(your-string);
JSONArray details=object.getJSONArray("details");
for(int j=0;j<details.length();j++){
JSONObject detail= details.getJSONObject(i);
String price = detail.getString("price");
....
}
JSONArray colors = object.getJSONArray("colors");
for(int i=0;i<colors.length();i++){
JSONObject obj= colors.getJSONObject(i);
// parse your json here
String color = obj.getString("color")
}
See following code:
private void decodeJSON()
{
String JSONString = "you json String";
try
{
JSONObject obj = new JSONObject(JSONString);
JSONArray arr = obj.getJSONArray("detail");
JSONObject detail = arr.getJSONObject(0);
int price = detail.getInt("price"); // do same thing to get other values
arr = obj.getJSONArray("colors");
JSONObject color = arr.getJSONObject(0);
String colorValue = color.getString("color");
String name = color.getString("name");
// do same thing for next object in array.
} catch (JSONException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Related
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
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/
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
}
}
This question already has answers here:
get the array index with value using arrayobject and store in array list
(2 answers)
Closed 10 years ago.
[ {
"question_id": 13,
"creation_date": 10,
"name": " certain degrees",
"tags": [
"android",
"sensor"
],
.....
...}
]
Here is the api I want to get the tags array ,the tag array contains only index and value,i want to get the array using array object and finally store in array list, i have tried but fail to get the error has been occured.
02-20 15:45:37.323: I/System.out(1165): org.json.JSONException: Value android at 0 of type java.lang.String cannot be converted to JSONObject
could anybody help me to solve this problem.
tagarray = questionobj.getJSONArray("tags");
for(int j=0;j<tagarray.length();j++){
//tagobject = tagarray.getJSONObject(j);//..if i comment this line the full array list is displaying but i need for each question allocated tags to display..am i clear..
//if(tagobject!=null){
tagname.add(tagarray.getString(j));
///tagname.add(0, null);
//}
}
your json format is not correct
try like that;
{"question":
{"question_id": "13",
"name": "certain degrees",
"tags": [{"android":"N", "sensor":"Y"}]
}}
You can use http://jsonviewer.stack.hu/, to be sure correction of your json data
I sugest you try this code,
import org.json.JSONArray;
import org.json.JSONObject;
public class Main {
public static void main(String[] args) {
String text = "[ { \"question_id\": 13, \"creation_date\": 10, \"name\": \"certain degrees\", \"tags\": [ \"android\", \"sensor\" ] } ]";
System.out.println(text);
JSONArray jsonArray = new JSONArray(text);
JSONObject jsonObjetc = jsonArray.getJSONObject(0);
JSONArray tagarray = jsonObjetc.getJSONArray("tags");
System.out.println(tagarray);
for(int j=0;j<tagarray.length();j++){
System.out.println(tagarray.getString(j));
}
}
}
Good luck, Aron
Try this,
ArrayList<String> list = new ArrayList<String>();
String text = "[ { \"question_id\": 13, \"creation_date\": 10, \"name\": \"certain degrees\", \"tags\": [ \"android\", \"sensor\" ] } ]";
try {
JSONArray jarray = new JSONArray(text);
for (int i = 0; i < jarray.length(); i++) {
JSONArray j_array = jarray.getJSONObject(i)
.getJSONArray("tags");
for (int j = 0; j < j_array.length(); j++) {
String str = j_array.getString(j);
list.add(str);
}
}
} catch (Exception e) {
}
Try This
tagarray = questionobj.getJSONArray("tags");
for(int j=0;j<tagarray.length();j++)
{
tagname.add(tagarray.getJSONObject(j).getString("android"));
}
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I want to parse the following JSON response. I couldn't extract the JSONArray which is inside the JSON object. I'm a novice to JSON parsing, any help would be appreciated.
{
"Result": {
"Data": [
{
"id": "1",
"Name": "ABC",
"release": "8",
"cover_image": "august.png",
"book_path": "Aug.pdf",
"magazine_id": "1",
"Publisher": "XYZ",
"Language": "Astrological Magazine",
"Country": "XYZ"
},
{
"id": "2",
"Name": "CDE",
"release": "8",
"cover_image": "august2012.png",
"book_path": "aug.pdf",
"magazine_id": "2",
"Publisher": "XYZ",
"Language": "Astrological Magizine",
"Country": "XYZ"
}
]
}
}
Basic code for implementing JSON Parsing is like:
JsonObject objJSON = new JSONObject("YourJSONString");
JSONObject objMain = objJSON.getJSONObject("NameOfTheObject");
JSONArray objArray = objMain.getJSONArray("NameOfTheArray"); // Fetching array from the object
Update:
Based on your comment, i can see you haven't fetched JSONArray "Data", without it you are trying to fetch values/attributes of a particular object:
JSONObject jObj = jsonObj.getJSONfromURL(category_url);
JSONObject menuObject = jObj.getJSONObject("Result"); String attributeId = menuObject.getString("Data");
String attributeId = menuObject.getString("Data"); // Wrong code
JSONArray objArray = menuObject.getJSONArray("Data"); // Right code
I like to use the GSON library: http://www.javacodegeeks.com/2011/01/android-json-parsing-gson-tutorial.html
It's a JSON-parsing library by Google.
OK, step by step:
String json = "{\"Result\":{\"Data\":[{\"id\":\"1\",\"Name\":\"ABC\",\"release\":\"8\",\"cover_image\":\"august.png\",\"book_path\":\"Aug.pdf\",\"magazine_id\":\"1\",\"Publisher\":\"XYZ\",\"Language\":\"Astrological Magazine\",\"Country\":\"XYZ\"},{\"id\":\"2\",\"Name\":\"CDE\",\"release\":\"8\",\"cover_image\":\"august2012.png\",\"book_path\":\"aug.pdf\",\"magazine_id\":\"2\",\"Publisher\":\"XYZ\",\"Language\":\"Astrological Magizine\",\"Country\":\"XYZ\"}]}}";
try
{
JSONObject o = new JSONObject(json);
JSONObject result = o.getJSONObject("Result");
JSONArray data = result.getJSONArray("Data");
for (int i = 0; i < data.length(); i++)
{
JSONObject entry = data.getJSONObject(i);
String name = entry.getString("Name");
Log.d("name key", name);
}
}
catch (JSONException e)
{
e.printStackTrace();
}
Json is hardcoded, so I had to escape it.
This code gets result object and then data array. A loop goes through an array and gets a value of Name.
I got in LogCat:
ABC
CDE
Note that you should surround it with try-catch or add throws to a method.