Json error org.json.JSONException: No value for - android

it gives me the error log : " org.json.JSONException: No value for tableData"
try {
JSONObject mainJson22 = new JSONObject(reply);
JSONObject jsonResult = mainJson22.getJSONObject("UserListByBusinessAreaContextResult");
JSONArray jsonArray22 = jsonResult.getJSONArray("tableData");
// JSONArray jsonArray22 = mainJson22.getJSONArray("UserListByBusinessAreaContextResult"); // JSONArray jsonArray22 = jsonResult.getJSONArray("tableData"); Log.i("mainjson234","" + jsonArray22);
for (int i2 = 0; i2 < jsonArray22.length(); i2++) {
JSONObject objJson22 = jsonArray22.getJSONObject(i2);
the error is on line : `"JSONArray jsonArray22 = jsonResult.getJSONArray("tableData");"
heres the json:
{
"UserListByBusinessAreaContextResult": "{\"tableData\":[{\"UserID\":30,\"Username\":\"Teste\",\"Name\":\"Teste\"}]}"
}

The object UserListByBusinessAreaContextResult contains a string. This string is:
"{\"tableData\":[{\"UserID\":30,\"Username\":\"Teste\",\"Name\":\"Teste\"}]}"
Do you notice the quotes? The problem is, that your WCF service returns a JSON string, not an object. So either you will have to change the WCF service to deliver an object instead, or parse that string again. Like this (not tested):
JSONObject mainJson22 = new JSONObject(reply);
JSONObject tableData = new JSONObject(
mainJson22.getString("UserListByBusinessAreaContextResult"));

Related

Unable to parse JSON object using Volley

JSON file:
JSON FILE URL
i am looping through the "biller" Array to fetch all the object's where "billerCategory": is
"Electricity". I am trying to get "paramName" value. But i am only getting 3 paramName values.
Response:
Code:
try {
JSONArray biller = response.getJSONArray("biller");
Log.d(TAG, biller.toString());
// Loop through biller Array and find billerID
for (int i = 0; i < biller.length(); i++)
{
JSONObject billerObj = (JSONObject) biller.get(i);
String category = billerObj.getString("billerCategory");
//Log.d(TAG, category);
if (category.equalsIgnoreCase("Electricity")){
JSONObject paraminput = billerObj.getJSONObject("billerInputParams");
JSONObject paramInfo = paraminput.getJSONObject("paramInfo");
String paramName = paramInfo.getString("paramName");
Log.d(TAG, paramName);
}
}
}
An exception has occured. Search for 'TORR00000SUR04'. It happened that "paramInfo" is an JSONArray at that point.

Why I am getting null JSON response?

I am fetching data from JSON to android.But, I am getting an empty JSON response. The PHP code which generates JSON data is as follows:
$result = $conn->query("SELECT dbname FROM users ORDER BY dbname ASC");
//defined second array for dbnames' list
$dblist = array();
while($row = $result->fetch_assoc()){
//array_push($response['dblist'],$row['dbname']);
$dblist[] = array('name'=>$row['dbname']);
}
$response['dblist'] = $dblist;
This is the JSON response.
{"dblist":[{"name":"a"},{"name":"arsod"}]}
The Java code to fetch data in android is as follows:
JSONObject obj = new JSONObject(s);
JSONArray names = obj.getJSONArray("dblist");
for(int i=0; i < names.length(); i++) {
JSONObject n = names.getJSONObject(i);
String name = n.getString("name");
Toast.makeText(MainActivity.this, name, Toast.LENGTH_SHORT).show();
institutes.add(name);
}
where institutes is an ArrayList in which I want to add each fetched element. But while fetching the data, I get the error in logcat org.json.JSONException: End of input at character 0 of. What is going wrong?
Considering the following:
s = {"dblist":[{"name":"a"},{"name":"arsod"}]}
Your code should be like,
JSONObject obj = new JSONObject(s);
JSONArray names = obj.getJSONArray("dblist");
for(int i=0; i < names.length(); i++) {
JSONObject n = names.getJSONObject(i);
String name = n.getString("name");
Toast.makeText(MainActivity.this, name, Toast.LENGTH_SHORT).show();
institutes.add(name);
}
remove this line:
JSONObject object = obj.getJSONObject("dblist");
and replace all occurences of object. with obj.
your JSONObject obj doesn't contain "dblist" object, there is only array inside it, so you should look for getJSONArray("dblist") straight inside obj
edit: you are parsing different String s, I've just checked this code:
final String s = "{\"dblist\":[{\"name\":\"a\"},{\"name\":\"arsod\"}]}";
JSONObject obj = new JSONObject(s);
JSONArray names = obj.getJSONArray("dblist");
for (int i = 0; i < names.length(); i++) {
JSONObject n = names.getJSONObject(i);
String name = n.getString("name");
Toast.makeText(this, name, Toast.LENGTH_SHORT).show();
}
which is almost exacly same as your and works pretty fine...

How can I put JSON return in an array?

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");
}

Simple Json Example with android

I want to read JSON web service result.
My web service result is
[{"etkinlikTarihi":123,"eposta":"posta","etkinlikAdi":"ali","etkinlikDetay":"veli","etkinlikId":1},{"etkinlikTarihi":12,"eposta":"posta","etkinlikAdi":"ali","etkinlikDetay":"detay","etkinlikId":2},{"etkinlikTarihi":13,"eposta":"posta","etkinlikAdi":"mali","etkinlikDetay":"detay","etkinlikId":3},{"etkinlikTarihi":13,"eposta":"posta","etkinlikAdi":"mali","etkinlikDetay":"detay","etkinlikId":4}]
But, whenever I try to read with JSONObject in Android, I get a exception...
My code is here,
jsonResponse = new JSONObject(wsEtkinlikListesi);
String etkinlikTarihi = jsonResponse.getString("etkinlikTarihi");
And this is my stack trace:
org.json.JSONException: Value [{"etkinlikAdi":"toplanti","etkinlikDetay":"toplan","etkinlikTarihi":1454648400000,"etkinlikId":5,"eposta":"sefagenel#gmail.com"}] of type org.json.JSONArray cannot be converted to JSONObject
Where is my mistake and how can I correct it?
First try to parse the array and then get every object on it:
JSONArray arr = new JSONArray(jSonResultString);
for (int i = 0; i < arr.length(); i++) {
JSONObject jsonobject = arr.getJSONObject(i);
String name = jsonobject.getString("etkinlikTarihi");
String url = jsonobject.getString("posta");
...
}

Value of type java.lang.String cannot be converted to JSONObject

Hello i am getting error in the follwing code as- "Value at reachus of type java.lang.String cannot be converted to JSONObject"..I am getting values for all other Json object but getting error at reachus object..how to convert java.lang.String to the JsonObject?please give the solution..Thanks
JSONObject jObj = new JSONObject(Constants.sercall.response_str);
JSONObject jObj2 = new JSONObject;
jObj2 = jObj.getJSONObject("value");
JSONObject jObj6=new JSONObject;
jObj6=jObj2.getJSONObject("reachus");
if (jObj6.has("Email")) {
activitycontactinfo = jObj6.getString("Email");
activitycontactinfo = URLDecoder.decode(activitycontactinfo);
activitycontactinfo= activitycontactinfo.replace("%20", " ");
activitycontactinfo = Constants.convertToUpperCase(activitycontactinfo);
}
Here is my Json:
{"value":{
"classdetails":{},
"other_activities_details":"",
"activitydetails":{},
"youlearn":"",
"reachus":{
"Email":[
"varsha.b%40wiztango.com"
],
"facebook":"http%253A%252F%252Fwww.facebook.com%252FVarsha%2520Borhade",
"website":"http%253A%252F%252Fwww.wiztango.com",
"linkedin":"http%253A%252F%252Fwww.linkedin.com%252Fborhadevarsha",
"twitter":"http%253A%252F%252Fwww.twitter.com%252Fvarshaborhade",
"phone number":[
{
"value":"678698",
"meta":"varsha"
}
]
},
"spons_speak_host":"",
"categories":"",
"display_blocks":[],
"display_user_information":[],
"display_content_information":[],
"display_published_contents":[],
"faculty_published_contents":[],
"open_url_enrollment_users_list":"0",
"total_participants":[],
"display_agenda_information":[],
"faculty_published_agenda":[],
"display_published_agenda_ids":[]
}
}
This code enables you to access the 'reachus' field and email:--
Use this code:--
JSONObject jsonObject = new JSONObject(s);
JSONObject reachusJson = jsonObject.getJSONObject("value")
.getJSONObject("reachus");
JSONArray emailArray = reachusJson.getJSONArray("Email");
Log.e("email ", emailArray.getString(0));
First, please use more meaningful names for your variables, then look at the structure of your JSON, it's:
JSONObject (main) -> JSONObject (value) -> JSONObject (reachus) -> JSONArray (Email) -> String
So:
Get your JSOBObject
JSONObject json = new JSONObject(Constants.sercall.response_str);
Get JSONObject for "value":
JSONObject valueJson = json.getJSONObject("value");
Get JSONObject for "reachus":
JSONObject reachusJson = json.getJSONObject("reachus");
Because "Email" is JSONArray, first get JSONArray, then get the first string (assuming there is one):
if (reachusJson.has("Email")) {
JSONArray emailJsonArray = reachusJson.getJSONArray("Email");
//rest of your code
activitycontactinfo = emailJsonArray.getString(0);
activitycontactinfo = URLDecoder.decode(activitycontactinfo);
activitycontactinfo = activitycontactinfo.replace("%20", " ");
activitycontactinfo = Constants.convertToUpperCase(activitycontactinfo);
}
You might want to add some checks in case Email array is empty.

Categories

Resources