Hi guys i have this JSONObject I'm having hard time to parse it.
Here's the JSON,
{
"query": {
"count": 2,
"created": "2016-11-07T15:29:47Z",
"lang": "en-US",
"results": {
"rate": [{
"id": "USDINR",
"Name": "USD/INR",
"Rate": "66.7700",
"Date": "11/7/2016",
"Time": "3:27pm",
"Ask": "66.8000",
"Bid": "66.7700"
}, {
"id": "USDEUR",
"Name": "USD/EUR",
"Rate": "0.9060",
"Date": "11/7/2016",
"Time": "3:29pm",
"Ask": "0.9065",
"Bid": "0.9060"
}]
}
}
}
As you can see above there are two "Rate" I want to store first "Rate" to a String array called Result[0] and second "Rate" to Result[1].
Here what I'm trying.
JSONObject object = new JSONObject(JSON);
JSONObject rateObject = object.getJSONObject("rate");
JSONArray rateArray = rateObject.getJSONArray("Rate");
result[0] = rateArray.getString(0);
result[1] = rateArray.getString(1);
I know maybe I'm doing this like stupid. So please help me out.
To Get the array I would try this:
JSONObject object = new JSONObject(JSON);
JSONObject rateObject = object.getJSONObject("results");
JSONArray rateArray = rateObject.getJSONArray("rate");
And then use a for to read all objects in the array:
int length = rateArray.length();
for (int i =0; i < length; i++){
JSONObject rate = rateArray.getJSONObject(i);
String rateResult = rate.getString("Rate");
}
Your formatted JSON looks like this
{
"query":{
"count":2,
"created":"2016-11-07T15:29:47Z",
"lang":"en-US",
"results":{
"rate":[
{
"id":"USDINR",
"Name":"USD/INR",
"Rate":"66.7700",
"Date":"11/7/2016",
"Time":"3:27pm",
"Ask":"66.8000",
"Bid":"66.7700"
},
{
"id":"USDEUR",
"Name":"USD/EUR",
"Rate":"0.9060",
"Date":"11/7/2016",
"Time":"3:29pm",
"Ask":"0.9065",
"Bid":"0.9060"
}
]
}
}
}
To get the rate array you have to this
JSONObject jsonObject = new JSONObject(jsonString);
JSONArray rateArray = jsonObject.getJSONObject("query").getJSONObject("results").getJSONArray("rate");
Then query rate array like this
JSONObject rateJSON = (JSONObject)rateArray.get(index);
String id = rateJSON.getString("id"); //Or any other field of rate
Anthony,
You need to go from the root element ( query ) to the "rate" node :
JSONArray rates = object.getJSONObject("query").getJSONObject("results").getJSONArray("rate")
result[0] = rates.getJSONObject(0).get("Rate")
result[1] = rates.getJSONObject(1).get("Rate")
you need to do this
JSONObject jsonObj = new JSONObject(jsonString);
JSONObject queryObj = jsonObj.getJSONObject("query");
JSONObject resultObj = queryObj.getJSONObject("results");
JSONArray rateArr = resultObj.getJSONArray("rate");
result[0] = rateArr.getJSONObject(0).getString("Rate");
result[1] = rateArr.getJSONObject(1).getString("Rate");
Try this:
JSONArray rateArray = new JSONObject(JSON)
.getJSONObject("query")
.getJSONObject("results")
.getJSONArray("rate");
result[0] = rateArray.getJSONObject(0).getString("Rate");
result[1] = rateArray.getJSONObject(1).getString("Rate");
Related
{
"kind": "youtube#channelListResponse",
"etag": "\"uQc-MPTsstrHkQcRXL3IWLmeNsM/RcyuNr1qwNrdSKCHUL-TlYislxI\"",
"pageInfo": {
"totalResults": 1,
"resultsPerPage": 1
},
"items": [
{
"kind": "youtube#channel",
"etag": "\"uQc-MPTsstrHkQcRXL3IWLmeNsM/06AiodZH5j6AmLIkxRzlAs2py9c\"",
"id": "UCfba9cyRs4aRiKaGi11d2Ig",
"statistics": {
"viewCount": "131077848",
"commentCount": "8",
"subscriberCount": "222353",
"hiddenSubscriberCount": false,
"videoCount": "5185"
}
}
]
}
This is json that i want to parse.
I want to parse viewCount, subscriberCount and videoCount.
JSONObject jsonObject1 = new JSONObject(jsonStr);
JSONArray jsonArray1 = jsonObject1.getJSONArray("items");
JSONObject jsonObject2 = jsonArray1.getJSONObject(0);
JSONArray jsonArray2 = jsonObject2.getJSONArray("statistics");
JSONObject jsonObject3 = jsonArray2.getJSONObject(0);
I tried like this but it shows error "JSONObject cannot be converted to JSONArray".
How can i get contents inside "statistics"?
(Sorry for my bad English)
"statistics" is a JSONObject instead of a JSONArray, that's where the error comes.
JSONObject jsonObject1 = new JSONObject(jsonStr);
JSONArray jsonArray1 = jsonObject1.getJSONArray("items");
JSONObject jsonObject2 = jsonArray1.getJSONObject(0);
JSONObject jsonObject3 = jsonObject2.getJSONObject("statistics");
String viewCount = jsonObject3.getString("viewCount");
String subscriberCount = jsonObject3.getString("subscriberCount");
String videoCount = jsonObject3.getString("videoCount");
Statistics are not JSON array, but JSON object.
statistic starts with {, so it's JSONObject, not JSONArray.
You try to getJSONArray("statistic") instead of getJSONObject("statistic")
Try this:
JSONObject jsonObject1 = new JSONObject(json);
JSONArray jsonArray1 = jsonObject1.getJSONArray("items");
JSONObject jsonObject2 = jsonArray1.getJSONObject(0);
JSONObject jsonObject3 = jsonObject2.getJSONObject("statistics");
And to read values, for example for commentCount:
int commentCount = jsonObject3.getInt("commentCount");
I can't seem to understand how to parse the "dispay_sizes" JSON array.
Here is my approach so far.
This is the JSON
{
"result_count": 6577719,
"images": [
{
"id": "505093872",
"asset_family": "creative",
"caption": "Professional footballer kicking ball during game in floodlit soccer stadium full of spectators under stormy sky and floodlights. It's snowing up there.",
"collection_code": "EPL",
"collection_id": 712,
"collection_name": "E+",
"display_sizes":
[{
"is_watermarked": false,
"name": "thumb",
"uri": "http://cache4.asset-cache.net/xt/505093872.jpg?v=1&g=fs1|0|EPL|93|872&s=1&b=RjI4"
}],
"license_model": "royaltyfree",
"max_dimensions": {
"height": 6888,
"width": 8918
},
"title": "Soccer player kicking a ball"
},
My code to parse it:
JSONObject imageData = new JSONObject(jsonData);
int resultCount = imageData.optInt("result_count");
Log.d("Result Count: ",Integer.toString(resultCount));
JSONArray imageArray = imageData.optJSONArray("images");
for( int i = 0; i < imageArray.length(); i++)
{
JSONObject img= imageArray.getJSONObject(i);
String id = img.optString("id");
allID.add(id);
Log.d("Image ID: ", id);
JSONArray imagePath = img.getJSONArray("display_sizes");
String pathURI = imagePath.getString("uri");
Log.d("Image Path: ",pathURI);
}
Can someone explain how to parse JSON array within a JSON array?
This
JSONArray imagePath = img.getJSONArray("display_sizes");
is right
[ // represents json array
{ // jsonobject
"is_watermarked": false,
"name": "thumb",
"uri": "http://cache4.asset-cache.net/xt/505093872.jpg?v=1&g=fs1|0|EPL|93|872&s=1&b=RjI4"
you have array of json object.
So you need
for(int j=0;j<imagePath.length();j++)
{
JSONObject jb = imagePath.getJSONObject(j);
String pathURI = jb.getString("uri");
}
In my application I'm trying to parse This structure type Json value. Did any of you guys face this issue? Any help would be greatly appreciated.
Main problem is double [[
[
[
{
"nid": "29",
"vid": "30",
"type": "oa_discussion_post",
"language": "und",
"title": "We want to hear from you.",
"uid": "1",
"status": "1",
"created": "1441316801",
"changed": "1442461699",
"comment": "2",
"promote": "0",
"sticky": "0",
"tnid": "0",
"translate": "0",
"uuid": "b9cb0351-5dbc-4ef1-8f8c-5570b66a2339"
}
]
]
Here is my approach:
JSONArray json = new JSONArray(jsonData);
for(int i=0;i<json.length();i++) {
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = json.getJSONObject(i);
String userType=e.getString("vid");
String topLine=e.getString("type");
}
Where is my error?
you have a nested array inside the top level array
for(int i=0;i<json.length();i++) {
JSONArray nestedJsonArray = json.optJSONArray(i);
if (nestedJsonArray != null) {
for(int j=0;j<nestedJsonArray.length();j++) {
// use j and nestedJsonArray to retrieve the JSONObect
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = nestedJsonArray.optJSONObject(i);
String userType=e.optString("vid");
String topLine=e.optString("type");
}
}
}
You have another JSONArraybut you use JSONObject. Just do
JSONArray firstArray = new JSONArray(jsonData);
JSONArray secondArray = firstArray.getJSONArray(0);
JSONObject jsonObject = secondArray.getJSONObject(0);
//Do want you want with your jsonObject
String userType = jsonObject .getString("vid");
String topLine = jsonObject .getString("type");
You have an object in an array in an array.
You have to select the first array in it instead of selecting the JSONObject directly. After selecting the second JSONArray, then select your JSONObject using getJSONObject(0);
i am doing now retrieving JSON array and assigning it to Android Array String but it failed.
My integer "success" already well. Please help, thank you very much.
My JSON information:
{
"details": [
{
"phone": "89898999",
"name": "Maria"
},
{
"phone": "98983555",
"name": "John"
},
{
"phone": "96969677",
"name": "Leo"
},
{
"phone": "97320099",
"name": "Helen"
},
{
"phone": "90063379",
"name": "Judy"
}
],
"success": 1
}
Android code:
String[] titleArray2;
String[] descriptionArray2;
int cs = 0;
.....
// Create a JSON object from the request response
JSONObject jsonObject = new JSONObject(result);
//Retrieve the data from the JSON object
cs = jsonObject.getInt("success")
JSONArray ja = jsonObject.getJSONArray("details");
for (int i = 0; i < ja.length(); i++)
{
JSONObject jo = (JSONObject) ja.get(i);
titleArray2[i] = jo.getString("phone");
descriptionArray2[i] = jo.getString("name");
}
Your code looks fine, Just try initializing your array
Try like this
JSONArray ja = jsonObject.getJSONArray("details");
titleArray2 =new String[ja.size];
descriptionArray2[i]=new String[ja.size];
and then your for loop.
Mark as right if it works for you. :)
Hey guys this is the first time I am doing JSON and I have gotten the json from the server. Now my only question is how do I take the data.
The JSON includes
[
{
"name": "Joe Smith",
"employeeId":1,
"company": "ABC",
"phone": {
"work": "555-555-5555",
"home": "666-666-6666",
"mobile": "777-777-7777"
}
},
{
"name": "Does Smith",
"employeeId":2,
"company": "XYZ",
"phone": {
"work": "111-111-1111",
"home": "222-222-2222",
"mobile": "333-333-3333"
}
}
]
jsonData is my string of the JSON
Currently I have:
JSONObject json = new JSONOBject(jsonData);
JSONObject data = json.getJSONObject(*******)
JSONArray phones = data.getJSONArray("phone");
not sure what to put on the second line. Also, what is the best way to group the information.
Please make it easy to understand, not exactly a professional yet haha
Thanks!
Your first line is wrong because current string is JSONArray of JSONObject's so get data from current string as:
JSONArray json = new JSONArray(jsonData);
for(int i=0;i<json.length();i++){
JSONObject data = json.getJSONObject(i);
// get name,employeeId,... from data
String str_name=data.optString("work");
//...
JSONObject jsonobjphones = data.getJSONObject("phone");
// get work from phone JSONObject
String str_work=jsonobjphones.optString("work");
//....
}
Try this
List list=new ArrayList();
JSONObject jObject = new JSONObject(str_response_starter);
JSONArray json_array_item_id = jObject.getJSONArray("itemid");
System.out.println("json array item id"+json_array_item_id);
JSONArray json_array_item_name = jObject.getJSONArray("itemname");
System.out.println("json array item name"+json_array_item_name);
JSONArray json_array_item_type = jObject.getJSONArray("type");
System.out.println("json array item type"+json_array_item_type);
JSONArray json_array_item_cost = jObject.getJSONArray("cost");
System.out.println("json array item cost"+json_array_item_cost);
Now
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(kvPairs.size());
String k, v;
Iterator<String> itKeys = kvPairs.keySet().iterator();
while (itKeys.hasNext()) {
k = itKeys.next();
System.out.println(" value of k"+k);
v = kvPairs.get(k);
System.out.println(" value of v"+v);
In JSON, [] represents JSONArray, {} represents JSONObject. In your JSON string, it starts with [, so you need to start with JSONArray:
JSONArray people = new JSONArray(jsonData);
And then you'll notice that each person is a JSONObject, so now you need to loop them in order to get their information:
for(int i=0;i<people.length();i++){
JSONObject person = people.getJSONObject(i);
JSONObject phones = person.getJSONObject("phone"); //phone is also a JSONObject, because it starts with '{'
String workPhone = phones.get("work");
}