I'm fairly new to Android Studio and Java, and I'm working on an app that takes data from Unsplash's API and displays it. However, I'm getting a JSON typeMismatch error, and I'm not sure how to correctly extract the data. Technically I'm getting back an array of JSONObjects, but I'm finding that simply changing JSONObject to JSONArray is not the correct approach.
I believe the problem is with the following lines of code: What I want to do is get the user (photographer) name and profile image, and the image they're posting.
Any help is greatly appreciated!
private NewPhotos getNewPhotos(String jsonData) throws JSONException {
JSONObject unsplash = new JSONObject(jsonData);
JSONObject user = unsplash.getJSONObject("user");
return new NewPhotos();
}
This is the JSON I'm getting back
This is the error message
You need first, cast JSON ARRAY.
You didn't put all json file, but it seems to be an array first.
private NewPhotos getNewPhotos(String jsonData) throws JSONException {
JSONArray unsplash = new JSONArray(jsonData);
for (int i = 0; i < unsplash.length(); i++) {
JSONObject jsonObj = (JSONObject) unsplash.get(i);
JSONObject user = jsonObj.getJSONObject("user");
// Do something with user
}
// Your implementation
return new NewPhotos();
}
This doesn't work for you? :
JSONObject unsplash = new JSONObject(jsonData);
JSONArray user = unsplash.getJSONArray("user");
Cause in your code you're trying to assign jsonArray to jsonObject.
You can't convery JSON Array into a Json object. Rafaela is right. You need to pass the string in Json array and then loop through each object to access user json.
Related
I am trying to extract the JSON data which is just a single word. I have used JSON Array when it was a long list of a set of data but this time it just a word like - done or failed.
My Code is -
JSONObject jsonObject = new JSONObject(s);
//JSONArray jsonArray = jsonObject.getJSONArray("contacted");
loggingTest = jsonObject.getString("??"); // what to put here (??) as
//there is just a single word.
It may be very easy to get it but I feels I am missing something. Thanks for your help in advance.
URL just gives a word like - done that's it not even " or : { nothing
Then simply use the String variable s , don't convert it into json
plus you can put this JSONObject jsonObject = new JSONObject(s); in try-catch to recognize that respose is simply a valid json or not , if not it will throw an exception
try{
// exception will be thrown in case of invalid json
JSONObject jsonObject = new JSONObject(s);
}
catch(JSONException e){
// s containing a single word so use it as it is
}
Well I'm new to Android. I'm getting a JSON string from a remote URL.
[{"key":"myString1","val":"myValue1"},{"key":"myString2","val":"myValue2"},{"key":"myString3","val":"myValue3"},{"key":"myString4","val":"myValue4"},{"key":"myString5","val":"myValue5"}]
I just need to parse this JSON string & display all key-val pair. I tried something like below from one of the tutorial.
JSONArray jArray = new JSONArray(str);
json = jArray.getJSONObject(0); //This will take first pair.
But I don't know the syntax for iterating through whole json object. Any help would be appreciated. Thanks in Advance.
There's nothing special in it. You do it like iterating any other array.
Let's say you have two String arrays to be filled with values: String[] mKey, mValue
Reading from JSON array will be like:
for (int i = 0; i < array.length(); i++) {
JSONObject object = array.getJSONObject(i);
mKey[i] = object.getString("key");
mValue[i] = object.getString("val");
}
I'm having a provblem on creating a JSON object on android app.
I's getting a JSON array like :
[{"estado":"1","longitude":"19.1714172","latitude":"-8.9477729"},{"estado":"1","longitude":"37.1714681","latitude":"-8.9476652"}]
from the server (using a servlet).
But when i try to access the array objects on a for cicle like:
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
//the rest of code
}
my app gives some error, and simple stop (something like an ANR! i'm not sure about this).
Can anyone help? please.
First, please remove the exclamation mark "!" at the end of your sentences, it might offend somebody here ^^
Okay, onto your problem, I suggest to encapsulate your JSON array inside a json object, maybe like this :
{"data":[{"estado":"1","longitude":"19.1714172","latitude":"-8.9477729"},{"estado":"1","longitude":"37.1714681","latitude":"-8.9476652"}]}
For example, we save that result string inside a String called jsontext, then we would create a JSONObject from that string, grab the JSONArray, and parse the JSONObject on the array. Here's a little snippet :
String jsontext = "yourjsontext";
//if it's from a http response, you might call respon
JSONObject mainObject = new JSONObject("jsontext");
JSONArray jsonArray = mainObject.getJSONArray("data");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
//the rest of code
} code here
Hope this helps, good luck ^^
Regards,
Reid
i need to parse the JSON data given below.
{"result":[{"bookId":142645,"bookpb":"MF",
"bookTs":1328999630000,"clipStatus":"D","bookDetail":{"arrival":1,"purchase":1,"sold":1},
"hierarchies":{"categories":["4"],"events":[]},"shopId":769752},
upto "sold" it is working fine.but when i am trying to parse categories it is not working.
given below is the code for parsing the data.
ArrayList<BookItem> resultdata = new ArrayList<BookItem>();
JSONArray jsonArray = (new JSONObject(inputString))
.getJSONArray("result");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
item = new BookItem();
item.setbookId(jsonObject.optString(book_ID));
item.setPurchase(jsonObject.optInt(PURCHASE));
item.setArrival(jsonObject.optInt(ARRIVAL));
item.setSold(jsonObject.optInt(SOLD));
item.setbookTs(jsonObject.optString(book_TS));
JSONObject hierarchies=jsonObject.getJSONObject(HIERARCHY);
item.setCategory(hierarchies.getInt("categories"));
resultdata.add(item);
}
can anybody help me???
i came to know that this is the problem of
{"categories":["4"],"events":[]}
data.how can i parse this array value?
categories is an JSONArray in order to get JSONArray
replace
item.setCategory(hierarchies.getInt("categories"));
with
item.setCategory(hierarchies.getJSONArray("categories").getInt(0));
For your purpose the best solution is probably GSON library. It do serialization and deserialization on its own and you will get your objects.
http://code.google.com/p/google-gson/
I got the Json response. I am not able to get the values from the string.my string is
Json_response is
{"NameAllList":[{"Current":{"time":"2012-02-21T08:04:21","Name":"abcd"},
"Next":{"Name":"data1","StartTime":"2012-02-21T08:06:21"}},{"Current":{"time":"2012-02-21T08:14:21","Name":"defg"},
"Next":{"Name":"data2","StartTime":"2012-02-21T08:24:21"}},{"Current":{"time":"2012-02-21T08:28:21","Name":"ghij"},
"Next":{"Name":"data3","StartTime":"2012-02-21T08:34:21"}},{"Current":{"time":"2012-02-21T08:40:21","Name":"knmo"},
"Next":{"Name":"data4","StartTime":"2012-02-21T08:48:21"}}]}
and i tried this.
JSONObject jsonObj = new JSONObject(json_response);
JSONObject subObj = jsonObj.getJSONObject("Current");
String name_current =subObj.getString("Name");
but i am not able to get the value of "Name". what mistake i have done. provide the link to do the above parsing.
first of all, your JSON response is having NameAllList as a JSON Array of objects.
So you have to fetch JSON Array first, then you can fetch one-by-one object.
for example:
JSONObject jsonString = (new JSONObject(json_response_string));
JSONArray array = jsonString.getJSONArray("NameAllList");
for(int i=0; i<array.length(); i++)
{
// Retrieve Current object as such
JSONObject objCurrent = array.getJSONObject("Current");
// Retrieve Next object as such
JSONObject objNext = array.getJSONObject("Next");
}
You are not parsing json properly, so you are not able to fetch value of Name. Please note JSON Annotation [] represent JSONArray, and {} respresent JSONObject, so method to get current item's name is:
JSONObject jsonObj = new JSONObject(json_response_string);
JSONArray jsonArr=jsonObj.getJSONArray("NameAllList");
String Hora_name_current="";
for(int i=0;i<jsonArr.length();i++)
{
JSONObject obj=jsonArr.get(i);
try{
JSONObject subObj = obj.getJSONObject("Current");
Hora_name_current =subObj.getString("Name");
break;
}catch(JSONException ex)
{
}
}
looks like you're trying to use JSONObject when you should be using JSONArray for the second request. Try this:
JSONObject jsonString = (new JSONObject(json_response_string));
JSONArray array = jsonString.getJSONArray("NameAllList");
In your JSON return, "NameAllList is actually an array and needs to be handled as such. Once you set it to "array", you can then run a for loop and treat it like any other array in Java.
Let me know if that helps.
David
JSONObject jsonObj = new JSONObject(json_response_string);
JSONArray jsonArray = jsonObj.getJSONArrays("NameAllList");