I am trying to parse the response i get from the network database. I am getting only one value i.e. seller id in response. When i try to pass it in JSON Object, somehow the toast after it doesn't execute and the toast after it is not executed.
I know there are many related questions but i can't find whats wrong... please help. Thanks.
try {
JSONArray jArray = new JSONArray(stuff.toString());
Toast.makeText(this, "len: " + jArray.length(), 1000).show(); // length is 1
for (int i = 0; i < jArray.length(); i++) {
Toast.makeText(this, "Arr: " + stuff, 1000).show();
// Arr: [{"seller_id":"5"}]
JSONObject jObject = jArray.getJSONObject(i);
j_id = jObject.getString(stuff);
// not getting executed
Toast.makeText(this, "JSON: " + j_id, 1000).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
Toast.makeText(this, "View: " + j_id, 1000).show(); // j_id is giving null value
This is my json data [{"seller_id":"5"}] i am trying to get the value of seller id (ie 5 here) out of it.
There is a problem in jObject.getString() method args
JSONObject jObject = jArray.getJSONObject(i);
j_id = jObject.getString(stuff);
Your giving the array. It should the name of the object i.e seller_id
so after the modification your code would be j_id = jObject.getString("seller_id");
After the following code:
j_id = jObject.getString(stuff);
add textview.settext(j_id);
and change this textview with your textview name
and if its not working than show your complete json data.
Related
I am trying to get results from this google books api "https://www.googleapis.com/books/v1/volumes?q=android&maxResults=20"
The code for Json parsing is
try {
JSONObject root = new JSONObject(booksJson);
JSONArray books = root.getJSONArray("items");
for (int i = 0; i < books.length(); i++) {
JSONObject book = books.getJSONObject(i);
JSONObject info = book.getJSONObject("volumeInfo");
String title = info.getString("title");
JSONArray authors = info.getJSONArray("authors");
String author = authors.getString(0);
JSONObject imageLinks = info.getJSONObject("imageLinks");
String imageLink = imageLinks.getString("smallThumbnail");
Book bookObject = new Book(author,title,imageLink);
booksList.add(bookObject);
Log.d(LOG_TAG + " Index :", String.valueOf(i) + " : " + String.valueOf(books.length()));
}
} catch (JSONException e) {
}
Problem is that there are 20 results when I check in web browser and it also shows 20 results when I log books.length() in logcat but It skips some books and shows fewer results as expected. Please Help.
You got less results because in for loop when you parse the data an exception occurred and you break the for loop . The data showed which the data added to your booksList before the exception occurred .
So Debug the for loop to detect which parameter you try to parse and not found so the exception occurred
Thanks
I have JSON Response like this.I have tried a lot but unable to parse the Values.
This one is my Actual Response...
{"ResponseCode":"000","ResponseDescription":"Successful","SystemServiceID":["0000"],"SystemServiceName":["Test"],"ProductID":["000"],"ProductName":["Test"],"ProductDescription":["Test product"],"MinimumValue":[10000],"MaximumValue":[500000],"ImageURL":[null],"Country":["AAAA"],"CompanyID":["1"],"CompanyName":["Test"],"FieldLevel":["2"],"FieldInfo":["{\"Field1\":{\"Field Name\":\"Phone Number\",\"Field Type\":\"Number\",\"Validation\":{\"Min\":\"4\",\"Max\":\"8\"}},\"Field2\":{\"Field Name\":\"Email\",\"Field Type\":\"String\",\"Validation\":{\"Regular Expression\":\"abcd\",\"Min Length\":\"10\",\"Max Length\":\"20\"}}}"]}
Out of this i am able to parse all the field expect the below one...
{
"Field1":
{
"Field Name":"Phone Number",
"Field Type":"Number",
"Validation":{"Min":"4","Max":"8"}
},
"Field2":
{
"Field Name":"Email",
"Field Type":"String",
"Validation":{"Regular Expression":"abcd","Min Length":"10","Max Length":"20"}
}
}
I also want to fetch the value of Validation":{"Min":"4","Max":"8"} this field.Like it has max value 4 and min value is 8.
Any Help will be appreciated.
Thanks in Advance ... :)
Here is the code to read Min And Max values
JSONObject parentObject = new JSONObject(Json_String);
JSONObject field1 = parentObject.getJSONObject("Field1");
JSONObject validation = field1.getJSONObject("Validation");
String min = validation.getString("Min");
String max = validation.getString("Max");
Assuming your JSON string is well formed.
Since in your response you are not getting the "\" into JSON so you need to some modifications into that like (update : It seems that replacement not required here)
String json = "{\"Field1\":{\"Field Name\":\"Phone Number\",\"Field Type\":\"Number\",\"Validation\":{\"Min\":\"4\",\"Max\":\"8\"}},\"Field2\":{\"Field Name\":\"Email\",\"Field Type\":\"String\",\"Validation\":{\"Regular Expression\":\"abcd\",\"Min Length\":\"10\",\"Max Length\":\"20\"}}}";
// json.replace("\\", ""); // I verified that parsing works well without replacement too
try {
JSONObject parentObject = new JSONObject(json);
JSONObject field1 = parentObject.getJSONObject("Field1");
JSONObject validation = field1.getJSONObject("Validation");
String min = validation.getString("Min");
String max = validation.getString("Max");
System.out.println("Min ::::::: " + min);
System.out.println("MAx ::::::: " + max);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
The below is called from a string url and returns a json object which has an json array inside, but something else is not working, can anyone show me how to access the data inside?
{"data":[{"8196":{"booking_id":"8150","client_id":"107","venue_id":null}}]
String jsonStr = "{\"data\":[{\"8196\":{\"booking_id\": \"8150\",\"client_id\": \"107\",\"venue_id\": null}}]}";
try {
JSONObject json = new JSONObject(jsonStr);
JSONArray array = json.getJSONArray("data");
for (int i = 0; i < array.length(); i++) {
JSONObject o = array.getJSONObject(i);
Iterator it = o.keys();
while (it.hasNext()) {
String name = (String) it.next();
o = o.getJSONObject(name);
int bookingId = o.getInt("booking_id");
int clientId = o.getInt("client_id");
int venueId = o.optInt("venue_id");
Log.v("TEST", String.format("Booking ID: %s -- Client ID: %s -- Venue ID: %s", bookingId, clientId, venueId));
}
}
} catch (JSONException e) {
e.printStackTrace();
}
I guess this is what you'll want. By the way, the JSON you posted is malformed. It's missing a } in the end.
You first need to decode the JSON string. The JSON sting you got in return is actually a serialized version of a JSON object.
You need to decode that string into a native Java Object.
There are a number of methods to this in Java. You can start by checking out the Java section at json.org.
I think the problem is that the value of key venue_id is null. You can replace null value with empty string(""), try it.
I have this below Json which I'm getting from a .Net Web service:
[["Developer","test","developer#test.com"]]
I checked with jsonlint validator and It shows above json fine.
Because it is not having any key I don't know how to parse it in Android.
Can someone please help me how to parse this json without key.
If it had keys, I would have tried this:
{"first":"somevalue","last":"someothervalue"} // Json response
JSONObject json_obj = new JSONObject(resp); //response is the json array
String first = json_obj.getString("first");
String last = json_obj.getString("last");
String test = "Hello! " + first + " " + last;
Hope someone helps :)
Regards,
Praveen
MY SOLUTION
JSONArray respo = new JSONArray(resp); //resp is Json Array
respo = respo.getJSONArray(0);
String test = respo.getString(2);
test = "Your Email Address is " + test;
in Android (Java) parsing this line looks like:
String jsonString="[[\"Developer\",\"test\",\"developer#test.com\"]]";
try {
JSONArray jsonArray=new JSONArray(jsonString);
JSONArray jsonArray2=jsonArray.getJSONArray(0);
for (int i = 0; i < jsonArray2.length(); i++) {
String valueString=jsonArray2.getString(i);
Log.e("json", i+"="+valueString);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Use JSONArray instead of JSONObject. Then use the getJSONArray() function to get the array inside the array. After that you can access all values with their index and getString().
I think it's very odd that the TextViews are not showing me any data that it has retrieved from the mySQL database. Here is my code:
This is the key parsing bit, I have two columns that need parsing and putting into separate TextView, something that I think I have done:
//PARSING JSON DATA
try {
jArray = new JSONArray(result);
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
Log.i("log_name", "Driver_full_name:" + json_data.getString("Driver_full_name"));
Log.i("log_for", "Drives for:" + json_data.getString("Drives_for"));
returnString += "\n\t" + jArray.getJSONObject(i);
somethingelse += "\n\t" + jArray.getJSONObject(i);
}
} catch (JSONException e1) {
Log.d("DB", "Error somewhere");
CurrentSeasonDrivers_DriverName.this.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(CurrentSeasonDrivers_DriversName, "Could not parse data so shut up",
Toast.LENGTH_LONG).show();
}
});
}
return returnString += somethingelse;
}
protected void onPostExecute(String returnString, String somethingelse) {
TextView drivername = (TextView) findViewById(R.id.DriverName);
drivername.setText(drivername.getText() + "\n" + returnString);
TextView drivesfor = (TextView) findViewById(R.id.DrivesFor);
drivesfor.setText(drivesfor.getText() + "\n" + somethingelse);
}
}
}
Hoping someone can help. Considering there are no errors. The logs in the parsing bit show the data fully parsed, however the logs in the onPostExecute show it unparsed again, or at least with curly brackets and speech marks around each row.
Thanks for any help. Really appreciate it.
Looks like you're parsing the JSON using the getString method for your logs, but stuffing the entire JSON in the Strings that you use to set the text. Unless there's something in between these steps that's not here, I think what you need to do is use the getString method when appending to returnString and somethingElse.
Is your onPostExecute method even called? The signature is wrong, it cannot take two parameters.
You should pass a String array instead (and of course change your AsyncTask class declaration to use a String array in onPostExecute) :
...
return new String[] {returnString, somethingelse};
}
protected void onPostExecute(String[] strings) {
String returnString = strings[0];
String somethingelse = strings[1];
}