I have a json object:
"images":{"1":{"imagename":"image1.gif","url":"image1url"},"2":{"imagename":"image2.gif","url":"image2url"},"3":{"imagename":"image3.gif","url":"image3url"}}
I want to fetch imagename and url from this. I enter into this images. I fetched the values 1,2,3 from images. But I am not able to fetch the json corresponding to this 1,2 and 3.
Its throwing exception stating: No value for 1
or No Value for 2
or No value for 3
What may be the reason for this cause? Please reply..
My present code is:
if(jsonObj.has("images")) {
JSONArray imagesArray = jsonObj.getJSONObject("images").names();
JSONObject imageDetailsObject;
for(int i = 0; i < imagesArray.length(); i++) {
imageDetailsObject = jsonObj.getJSONObject(imagesArray.get(i).toString());
if(imageDetailsObject.has("imagename")) {
//perform some actions
}
if(imageDetailsObject.has("url")) {
//perform some actions
}
}
}
EDITED:
imageDetailsObject = jsonObj.getJSONObject("images").getJSONObject(imagesArray.get(i).toString());
I got it worked by giving:
if (jsonObj.has("images")) {
JSONArray imagesArray = jsonObj.getJSONObject("images").names();
JSONObject imageDetailsObject;
for (int i = 0; i < imagesArray.length(); i++) {
imageDetailsObject = jsonObj.getJSONObject("images").getJSONObject(
imagesArray.getString(i));
if(imageDetailsObject.has("imagename")) {
//perform some actions
}
if (imageDetailsObject.has("url")) {
//perform some actions
}
}
}
Related
I have a JSONArray of JSONObjects in each of which is a string with the key "kind". I'm trying to loop through the JSONarray and organize them by "kind" to use to fill a SeperatedListAdapter. what I am trying to achieve is this structure
{
communications[
{ "header":"kind"
communications[
{}
{}
{}
{}
]
}
{ "header":"kind"
communications[
{}
{}
{}
{}
]
}
]
}
The problem I'm having is the for loop i have created to achieve this gets stuck in an endless loop and thus crashes the application. Does anyone know where I am going wrong with this?
Here's my code I know it gets stuck in a loop as it runs the log "1" about 1000 times even though I know there's only 5 things in the array I'm passing in.
for(int i = 0; i < communicationsFromFile.length(); i++){
JSONObject communication = communicationsFromFile.getJSONObject(i);
Log.v("Communications", "kind = " + communication.getString("kind"));
Log.v("Communications", "newComArray = " + newComArray);
if(newComArray.length() != 0){
Log.v("Communications", "newComArray IsNotempty");
for(int a = 0; a < newComArray.length();a++){
JSONObject itemInArray = newComArray.getJSONObject(a);
JSONArray communicationsForKind = itemInArray.getJSONArray("communications");
Log.v("Communications", "1");
if(itemInArray.getString("header").equals(communication.getString("kind"))){
Log.v("Communications", "Header Exists");
communicationsForKind.put(communication);
itemInArray.put("communications", communicationsForKind);
newComArray.put(itemInArray);
}else{
Log.v("Communications", "Header Does not Exist");
JSONObject addItemInArray = new JSONObject();
JSONArray addArrayToItem = new JSONArray();
addArrayToItem.put(communication);
addItemInArray.put("header", communication.getString("kind"));
addItemInArray.put("communications", addArrayToItem);
newComArray.put(addItemInArray);
}
}
}else{
JSONObject addItemInArray = new JSONObject();
JSONArray addArrayToItem = new JSONArray();
addArrayToItem.put(communication);
addItemInArray.put("header", communication.getString("kind"));
addItemInArray.put("communications", addArrayToItem);
newComArray.put(addItemInArray);
}
}
In each iteration of the inner for loop, you put new objects in the newComArray. The condition a < newComArray.length() is always satisfied because a and the array length both grow at the same rate.
I am working in Android just for a heads up. I was able to retrieve JSON data but now I have come to the point where there are multiples. Example:
"workers":[
{
"id":3357,
"username":"Unreliable.worker",
"password":"x",
"monitor":0,
"count_all":41,
"count_all_archive":0,
"hashrate":477,
"difficulty":106.56
},
{
"id":4061,
"username":"Unreliable.worker2",
"password":"x",
"monitor":0,
"count_all":0,
"count_all_archive":null,
"hashrate":0,
"difficulty":0
}
would I have to do a for loop or is there another way to do it?
This is the code that I am using now to get them:
JSONObject workersJSONObject = personalJSONObject.getJSONObject("workers");
but I don't know how I would get for each and separate them. I'll get them by using:
id= workersJSONObject.getDouble("id");
[] in JSON refers to a JSON array. You could use something like:
JSONArray workersJSONArray = personalJSONObject.getJSONArray("workers");
Then loop through each item as (basically get each object inside array):
for (int i = 0; i < workersJSONArray.length(); i++) {
JSONObject workersJSONObject = workersJSONArray.getJSONObject(i);
// parse object just like before
}
On JSON, {} defines an object, [] defines an array instead. if you want details. see this and JSONArray
sample:
JSONArray workersJSOnArray = new JSONArray(stringData);
or
JSONArray workersJSOnArray = personalJSONObject.getJSONArray("workers");
/* THen you can loop from each data you want */
int len = workersJSOnArray.length();
for (int i = 0; i < len; i++)
{
JSONObject item = workersJSOnArray.optJSONObject(i);
int id = item.getInt ("id");
.....
}
I am trying to parse a result (JSON) fetched from the Facebook API using FQL in an Android Application.
I have been able to parse all of the result set except this part:
[10151392619250579,10151392618640579,10151392618590579,10151392618785579,10151392618835579,10151392618885579,10151392619010579,10151392619155579]
The FQL query I am making is:
SELECT app_data FROM stream WHERE filter_key in (SELECT filter_key FROM stream_filter WHERE uid = me() AND type = 'newsfeed') AND is_hidden = 0 LIMIT 200
Which returns a result like this:
{
"app_data": {
"attachment_data": "[]",
"images": "[10151392619250579,10151392618640579,10151392618590579,10151392618785579,10151392618835579,10151392618885579,10151392619010579,10151392619155579]",
"photo_ids": [
"10151392619250579",
"10151392618640579",
"10151392618590579",
"10151392618785579",
"10151392618835579",
"10151392618885579",
"10151392619010579",
"10151392619155579"
]
}
}
And this is the code I have used to fetch the data:
// GET THE APP DATA
if (JOFeeds.has("app_data")) {
String strAppData = JOFeeds.getString("app_data");
if (strAppData.equals("[]")) {
// DO NOTHING
} else {
JSONObject JOAppData = new JSONObject(strAppData);
if (JOAppData.has("photo_ids")) {
String strPhotoIDS = JOAppData.getString("photo_ids");
JSONArray JAPhotoIDS = new JSONArray(strPhotoIDS);
Log.e("JAPhotoIDS", JAPhotoIDS.toString());
for (int j = 0; j < JAPhotoIDS.length(); j++) {
JSONObject JOPhotoIDS = JAPhotoIDS.getJSONObject(j);
Log.e("PHOTO IDS", JOPhotoIDS.toString());
}
}
}
}
The logcat however, always shows this error:
12-13 15:54:36.390: W/System.err(5841): org.json.JSONException: Value 10151392619250579 at 0 of type java.lang.Long cannot be converted to JSONObject
Clearly I am wrong in the coding. Can anyone provide any suggestions on what the proper approach / code should be?
the part where you are parsing the photo_ids is wrong, it should be like:
if (JOAppData.has("photo_ids")) {
JSONArray JAPhotoIDS = JOAppData.getJSONArray("photo_ids");
Log.e("JAPhotoIDS", JAPhotoIDS.toString());
for (int j = 0; j < JAPhotoIDS.length(); j++) {
String id = JAPhotoIDS.getString(j);
Log.e("PHOTO IDS", id);
}
}
Your JSONArray JAPhotoIDS includes an array of Long (not JSONObject).
So instead using
JSONObject JOPhotoIDS = JAPhotoIDS.getJSONObject(j);
use
Long lPhotoIDS = JAPhotoIDS.getLong(j);
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Comparing two identical strings with == returns false
I am having real hard time with solving this code. This might look silly but I am not able to figure out what is happening. I am parsing a JSONArray(No big deal!) I am traversing the array with a for loop. I want to break the loop when the user input and the value matches. Here is my code
String regi = null;
JSONObject studentObject = null;
try {
JSONArray returned = test.getInternetData();
int i;
for (i = 0; i < returned.length(); i++) {
studentObject = returned.getJSONObject(i);
regi = studentObject.getString("REGISTRATION_NO");
if (regi == reg) {
name = studentObject.getString("STUDENT_NAME");
break;
}
}
course = studentObject.getString("COURSE_CODE");
Log.d("Details", name + course + regi + i);//Out put: nullGraduate081018394:name - null
//course: Graduate (same for all), regi: last registration number,
//i: giving totalnumber of objects
As per my knowledge the loop should stop when it finds a match. The COURSE_CODE will be corresponding to the student. Am I missing something?
Please note: The function getInternetData() is returning the whole JSON Array. The loop is completely traversing every object.
Strings cannot be compared with == in Java. You have to use string1.equals(string2).
Use regi.equals(reg) or regi.contentEquals(reg) instead of == and you will be fine :-)
use regi.contentEquals(reg) or !regi.contentEquals(reg) for comparison
you should use regi.contentEquals(reg)
try using this
JSONArray returned = test.getInternetData();
int i;
for (i = 0; i < returned.length(); i++) {
// added the below line
studentObject = new JsonObject();
studentObject = returned.getJSONObject(i);
regi = studentObject.getString("REGISTRATION_NO");
if (regi.equals(reg)) {
name = studentObject.getString("STUDENT_NAME");
break;
}
}
instead of just
JSONArray returned = test.getInternetData();
int i;
for (i = 0; i < returned.length(); i++) {
studentObject = returned.getJSONObject(i);
regi = studentObject.getString("REGISTRATION_NO");
if (regi == reg) {
name = studentObject.getString("STUDENT_NAME");
break;
}
}
In my app, i want to parse json response which is in the format
{"quote":[{"orderId":"3209926"},{"totalpages":1}]}
below is the code which i had done,But the problem is how to get the "totalpages" value?
try {
JSONObject jObject = new JSONObject(result);
JSONArray jArray = jObject.getJSONArray("quote");
for (int i = 0; i < jArray.length(); i++)
{
JSONObject offerObject = jArray.getJSONObject(i);
current.orderId = offerObject.getInt("orderId");
It shows error when i use
current.totalpage= offerObject.getInt("totalpages");
Anybody knows how to parse this?THanks in advance
Note that getInt(), like other get-functions of JSONObject throw JSONException if the object does not contain the key requested. Thus, before you request the key you should use hasKey() to determine whether the object contains the key.
For example, inside the for loop you can do the following:
JSONObject offerObject = jArray.getJSONObject(i);
if(offerObject.has("orderId") {
current.orderId = offerObject.getInt("orderId");
}
if(offerObject.has("totalpages") {
current.totalpage= offerObject.getInt("totalpages");
}
You can also add a flag and a check after the loop to ensure that both orderId and totalpages were present in the JSON data.
I dont know why your json is having that structure. But if you want to parse it then you will have to do something like the following with the has function.
for (int i = 0; i < jArray.length(); i++) {
JSONObject offerObject = jArray.getJSONObject(i);
if(offerObject.has("orderId")) {
current.orderId = offerObject.getInt("orderId");
} else if(offerObject.has("totalpages")) {
current.totalpage= offerObject.getInt("totalpages");
}
}