I have GetRequest and as a response I got HTML head and the content of body element is Array of JSONS. Iam unable to parse it, could anyone post a tip how to create JSON Object from this response??
If you have your JSON return value in String json you can do
JSONArray a = new JSONArray(json);
The first JSONObject in the array can then be retrieved with
JSONObject o = a.getJSONObject(0);
You can create a JSON Object from a string like this.
String myString = "This is the response from your HTTP GET request";
JSONObject myJson = new JSONObject(myString);
Note that the value of myString must be valid JSON.
My problem was, when I downloaded request from server I have to get only content of element. Than I used standard JSONArray and everything is working fine.
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
try {
// Getting Array of Contacts
contacts = json.getJSONArray(TAG_CONTACTS);
// looping through All Contacts
for(int i = 0; i < contacts.length(); i++){
JSONObject c = contacts.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
String email = c.getString(TAG_EMAIL);
String address = c.getString(TAG_ADDRESS);
String gender = c.getString(TAG_GENDER);
// Phone number is agin JSON Object
JSONObject phone = c.getJSONObject(TAG_PHONE);
String mobile = phone.getString(TAG_PHONE_MOBILE);
String home = phone.getString(TAG_PHONE_HOME);
String office = phone.getString(TAG_PHONE_OFFICE);
}
} catch (JSONException e) {
e.printStackTrace();
}
Related
I'm a android beginner and I'm doing to access a JSON file in and it has an error. I have a problem in parsing this
JSONObject jsonObject = new JSONObject(jsonStr);
JSONArray accounts = jsonObject.getJSONArray("account_data");
for(int i=0;i < accounts.length();i++){
JSONObject a = accounts.getJSONObject(i);
sin = a.getString("sin");
account_name = a.getString("account_name");
address = a.getString("address");
status = a.getString("status");
due_date = a.getString("due_date");
total_amount = a.getDouble("total_amount");
sin_lbl.setText(a.getString("account_name"));
}
here is the JSON File
{"account_data":{
"sin":"200111-102 ",
"account_name":"LUMABAN, CRISTOM ",
"address":"352 MABINI ST.,, SABANG, Baliwag ",
"status":"A ",
"due_date":"2019-04-23",
"total_amount":"491.00"
},"code":1101,"message":"Account Info Retrieved"}
I have an error in putting it in array.
Instead of using JSONArray , try to use JSONObject.
String[] array = {json.get("sin"), json.get("account_name"), json.get("address"), json.get("status"), json.get("due_date"), json.get("total_amount") }
{"account_data":{"sin":"200111-102 ","account_name":"LUMABAN, CRISTOM ","address":"352 MABINI ST.,, SABANG, Baliwag ","status":"A ","due_date":"2019-04-23","total_amount":"491.00"},"code":1101,"message":"Account Info Retrieved"}
Actually, it's a json object, not array. So that you can not convert json object to json array
Difference between Json Array and Json Object:
A JSONArray is an ordered sequence of values. A JSONObject is an unordered collection of name/value pairs.
JSONArray: Its external text form is a string wrapped in square brackets with commas separating the values.
JSONObject: Its external form is a string wrapped in curly braces with colons between the names and values, and commas between the values and names.
Please use this json parshing
try {
JSONObject jsonObject = new JSONObject(jsonStr);
JSONObject accounts = jsonObject.getJSONObject("account_data");
sin = accounts.getString("sin");
account_name = accounts.getString("account_name");
address = accounts.getString("address");
status = accounts.getString("status");
due_date = accounts.getString("due_date");
total_amount = accounts.getDouble("total_amount");
sin_lbl.setText(a.getString("account_name"));
} catch (Exception e) {
}
if you asked about iterating on json object you could try this one
JSONObject jObject = new JSONObject(jsonStr);
JSONObject menu = jObject.getJSONObject("account_data");
Map<String,String> map = new HashMap<String,String>();
Iterator iter = menu.keys();
while(iter.hasNext()){
String key = (String)iter.next();
String value = menu.getString(key);
map.put(key,value);
}
so now you have your data into as pair of key and value
if you have a json array of this response you could do as following
JSONObject root = new JSONObject("your root");
JSONArray resultArray = root.getJSONArray("your array key");
for (int i = 0; i < resultArray.length(); i++) {
// here to get json object one by one and access every item into it
JSONObject resultObject = resultArray.getJSONObject(i);
posterPath = resultObject.getString("key");
title = resultObject.getString("key");
releaseDate = resultObject.getString("key");
description = resultObject.getString("key");
voteAverage = resultObject.getDouble("key");
}
i need help to parse this json code to actual strings using android volley. this is the json code:[{"name":"Tayo","0":"Tayo","thread_name":"Welcome","1":"Welcome","post":"Hi there,","2":"Hi there,","post_time":"Sunday","3":"Sunday"},{"name":"Pete","0":"Pete","thread_name":"Welcome","1":"Welcome","post":"Hi,am pete","2":"Hi,am pete","post_time":"Monday","3":"Monday"}].
I have tried other helps but not working. Thanks!
Remember:
if the .json content starts with { is considered as a Json Object.
if the .json content starts with [ is considered as a Json Array.
so you hava a JsonArray then you can parse your content like this way:
//Obtain the JsonArray
JSONArray jsonArray = new JSONArray(myJsonContent);
// Get the JsonObjects inside JsonArray
for(int i=0;i<jsonArray.length();i++){
JSONObject jsonobject = jsonArray.getJSONObject(i);
}
// strData is the json data received.
JSONArray jsonArray = new JSONArray(strData);
for (int i=0; i<jsonArray.length();i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
String name = jsonObject.optString("name");
String zero = jsonObject.optString("0");
String thread_name = jsonObject.optString("thread_name");
String one = jsonObject.optString("1");
String post = jsonObject.optString("post");
String two = jsonObject.optString("2");
String post_time = jsonObject.optString("post_time");
String three = jsonObject.optString("3");
//Just an example
arrayName.add(jsonObject.optString("name"));
}
You can even use array to store data instead of the string.
I have the following JSON
{"DistributorDealerList":
{[{"Id":2,
"Name":"Distributor1",
"Dealer":
[{"Id":"1",
"Name":"Dealer1"},
{"Id":"2","
Name":"Dealer2"}]},
{"Id":4,"Name":"Distributor2",
"Dealer":
[{"Id":"3",
"Name":"Dealer3"}]}]}
When I am parsing the JSON getting the below exception
org.json.JSONException: Names must be strings, but [{"Name":"Distributor1",
"Dealer":[{"Name":"Dealer1","Id":"1"},{"Name":"Dealer2","Id":"2"}],"Id":2},
{"Name":"Distributor2","Dealer":[{"Name":"Dealer3","Id":"3"}],"Id":4}] is of
type org.json.JSONArray at character 195 of {"DistributorDealerList":
{[{"Id":2,"Name":"Distributor1","Dealer":[{"Id":"1","Name":"Dealer1"},
{"Id":"2","Name":"Dealer2"}]},{"Id":4,"Name":"Distributor2","Dealer":
[{"Id":"3","Name":"Dealer3"}]}]}
HERE is my parsing code:
try {
JSONObject jsonObj = new JSONObject(apiResult);
// Getting JSON Array node
JSONArray contacts = jsonObj
.getJSONArray("DistributorDealerList");
// looping through All Contacts
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
String distId = c.getString("Id");
String distName = c.getString("Name");
JSONObject phone = c.getJSONObject("Dealer");
String distDealerID = phone.getString("Id");
String distDealerName = phone.getString("Name");
}
} catch (JSONException e) {
e.printStackTrace();
}
It says that I must supply the JSON Araay Name. Though I am supplying it but still the exception. What is wrong here.
Thanks #Sufian
http://json.parser.online.fr/
This does solve my problem
You should change your json. Your json data's id parameters doesn't have " character;
Control your id paramerters.
{"DistributorDealerList":
{[{"Id":"2",
"Name":"Distributor1",
"Dealer":
[{"Id":"1",
"Name":"Dealer1"},
{"Id":"2","
Name":"Dealer2"}]},
{"Id":"4","Name":"Distributor2",
"Dealer":
[{"Id":"3",
"Name":"Dealer3"}]}]}
The json provided by you is wrong.
I have solved you error in your json string
You can get this right json string from : http://pastie.org/private/1xbyzgamzihswnpgtz15yw
You can verify this json string using this url :
http://jsonviewer.stack.hu/
I hope it was helpfull.
I am new to android and JSON parsing. Here i have my json response that i get. Now i have several such response in my jsonarray. What i would like to know is how do i fetch a single value from this response.
i.e how can i fetch only "id" from these response:
{"id":"c200","gender":"male","phone":{"office":"00 000000","home":"00 000000","mobile":"+91 0000000000"},"address":"xx-xx-xxxx,x - street, x - country","email":"ravi#gmail.com","name":"Ravi Tamada"}
{"id":"c201","gender":"male","phone":{"office":"00 000000","home":"00 000000","mobile":"+91 0000000000"},"address":"xx-xx-xxxx,x - street, x - country","email":"johnny_depp#gmail.com","name":"Johnny Depp"}
I want only id of both these respones.
My code is
JSONObject jobject = jparse.getJSONFromUrl(url);
contacts = jobject.getJSONArray(TAG_CONTACTS);
for(int i = 0 ; i < contacts.length() ; i++)
{
JSONObject c = contacts.getJSONObject(i);
String id = c.getString(TAG_ID);
Log.i("TAG", "STRING VALUE:" + contacts.getString(i));
String name = c.getString(TAG_NAME);
String email = c.getString(TAG_EMAIL);
String address = c.getString(TAG_ADDRESS);
String gender = c.getString(TAG_GENDER);
}
If you dont want all id then remove for loop and just put
JSONObject j = contacts.getJSONObject(0);
String id = j.getString("id");
it will give the first object id that is only : c200
You got response from server in as json.
You can get only id as shown below code.
JSONObject json= new JSONObject(response);//response= your json string.
String id = json.getString("id");//id = param in your response
I am working on an Android project and my json strings are a bit strange, all of the tutorials are showing that I need to parse an JSONArray but my JSON has no array name.
Heres an example json url. (my understanding says that there needs to be something like "article" before the [
[
///Something should be here
{
"id": 15483,
"title": "Bilbo Baggins is Cool",
"permalink": "http://example.com/2012/12/03/",
"content": "Hello World",
"date": "2012-12-03 00:04:08",
"author": "Bilbo Baggins",
"thumbnail": "http://example.com/wp- content/uploads/2012/12/DSC02971.jpg",
"categories": [
"News"
],
"tags": [
"LOTR",
"One Ring",
"Patch",
"Police Department"
]
}
]
Like this example http://www.androidhive.info/2012/01/android-json-parsing-tutorial/
They have a tag at the beginning called "contacts" which lets him use a JSONArray to loop through all the contents and get the tags...
I am lost.
How can I parse this data? the tutorial code would not work as I cannot pull an array(as least i think).
I am lost
First of all,I would like to tell you that in the example of androidhive,the root element of jsonstring is the jsonobject and the root element of jsonstring which you posted as question is the jsonarray.
Second of all,It's not necessary to always have a name at the beginning of array to consume it and extract data from it.but yes,it surely requires in complex cases and it is good practice too.
How can I parse this data?
the root element is different,so you need to change the way of consuming the data,
in example of androidhive,
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url); //look at the left side of assignment operator.here result is being consumed in JSONObject
try {
// Getting Array of Contacts
contacts = json.getJSONArray(TAG_CONTACTS);
// looping through All Contacts
for(int i = 0; i < contacts.length(); i++){
JSONObject c = contacts.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
String email = c.getString(TAG_EMAIL);
String address = c.getString(TAG_ADDRESS);
String gender = c.getString(TAG_GENDER);
// Phone number is agin JSON Object
JSONObject phone = c.getJSONObject(TAG_PHONE);
String mobile = phone.getString(TAG_PHONE_MOBILE);
String home = phone.getString(TAG_PHONE_HOME);
String office = phone.getString(TAG_PHONE_OFFICE);
}
} catch (JSONException e) {
e.printStackTrace();
}
the jsonstring which you posted as question can be consumed in JSONArray,
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONArray json = jParser.getJSONFromUrl(url); //look at the left side of assignment operator.here result is being consumed in JSONArray
try {
// looping through All data
for(int i = 0; i < json.length(); i++){
JSONObject c = json.getJSONObject(i);
// Storing each item in variable
String id = c.getString("id");
String title= c.getString("title");
String permalink= c.getString("permalink");
String content= c.getString("content");
}
} catch (JSONException e) {
e.printStackTrace();
}
You can see this post, it seems that you have a similar JSONArray.. you can simply use:
JSONArray yourArray = new JSONArray(jsonString);
// do the rest of the parsing by looping through the JSONArray