JSON array and object in Android - android

Parsing JSON array and object in Android
json:https://api.adjaranet.com/api/v1/movies/
I am trying to parse it with the following Java code in Android but unlimited loading when add
moviejson.getJSONObject("genres").getJSONObject("data").getString("primaryName")
half code is
try {
JSONObject jsonObject = new JSONObject(content);
JSONArray jsonArray = jsonObject.getJSONArray("data");
for(int i =0;i<jsonArray.length(); i++){
JSONObject moviejson = jsonArray.getJSONObject(i);
//if (moviejson.getJSONObject("plot").getJSONObject("data").getString("language").equals("GEO")){
arrayList.add(new MovieItem(
moviejson.getString("id"),
moviejson.getJSONObject("posters").getJSONObject("data").getString("240"),
moviejson.getString("primaryName"),
moviejson.getString("secondaryName"),
moviejson.getString("year"),
moviejson.getJSONObject("plot").getJSONObject("data").getString("description"),
moviejson.getJSONObject("rating").getJSONObject("imdb").getString("score"),
moviejson.getJSONObject("covers").getJSONObject("data").getString("1920"),
moviejson.getJSONObject("genres").getJSONObject("data").getString("primaryName")
));
//}
loading.setVisibility(View.GONE);
}
} catch (JSONException e) {
e.printStackTrace();
}
How solve this problem?

In your json content, data inside genres is an array, not object. That's why when you are trying to parse moviejson.getJSONObject("genres").getJSONObject("data") it throws error.
Try
String genreString = "";
int length = moviejson.getJSONObject("genres").getJSONArray("data").length();
for(int j =0 ; j < length ; j++) {
genreString += moviejson.getJSONObject("genres").getJSONArray("data").getJSONObject(j).getString("primaryName");
}

Related

org.json.JSONException: Value Error Android

I am pulling the data from the array I have assigned in the php file. I get an error pulling data from the Array output. The output is as follows:
org.json.JSONException: Value [{"program_dosya":"..."},{"program_dosya":"https:..."}, `{"program_dosya":"https:\/\/..."}] of type org.json.JSONArray cannot be converted to JSONObject`
I want to pull the "programfile" data assigned in array via android.
PHP
$results[] = Array("program_dosya" => $programdosya);
Android
try {
JSONObject jObj = new JSONObject(result);
JSONArray resultArray = jObj.getJSONArray("");
for(int i = 0; i < resultArray.length(); i++) {
JSONObject obj = resultArray.getJSONObject(i);
String name = obj.getString("programdosya");
}
} catch (JSONException e) {
e.printStackTrace();
}
As I understand you need to iterate over the json array -
// assuming you have string in this format, then all you have to do is
String result = "[{\"program_dosya\":\"...\"},{\"program_dosya\":\"https:...\"}]";
JSONArray array = new JSONArray(result);
for (int i=0; i<array.length(); i++) {
JSONObject js = array.getJSONObject(i);
String link = js.getString("program_dosya");
System.out.println(link);
}

How to parse this type of json array in android?

I have know to parser JSON array in single array but how to pass multiple JSON array and set it value to require
{"scode":"200","all_menu":[{"app_menu_id":"67","app_menu_name":"Demograpics","all_sub_menu":[{"app_menu_id":"67","app_sub_menu_id":"47","app_sub_menu_name":"\u0a97\u0ac1\u0a9c\u0ab0\u0abe\u0aa4\u0ac0","app_sub_menu_image":"http:\/\/app.hindipublic.com\/app_allwhatsupstatus\/menu\/medium\/8451504072003.jpg"}],"sub_menu":"true"},{"app_menu_id":"68","app_menu_name":"Lyrics","all_sub_menu":[{"app_menu_id":"68","app_sub_menu_id":"48","app_sub_menu_name":"Music","app_sub_menu_image":"http:\/\/app.hindipublic.com\/app_allwhatsupstatus\/menu\/medium\/4681504072092.jpg"}],"sub_menu":"true"},{"app_menu_id":"69","app_menu_name":"Adult","all_sub_menu":[{"app_menu_id":"69","app_sub_menu_id":"49","app_sub_menu_name":"Double
Meaning","app_sub_menu_image":"http:\/\/app.hindipublic.com\/app_allwhatsupstatus\/menu\/medium\/9931504072151.jpg"}],"sub_menu":"true"},{"app_menu_id":"70","app_menu_name":"Emotions","all_sub_menu":[{"app_menu_id":"70","app_sub_menu_id":"50","app_sub_menu_name":"Love","app_sub_menu_image":"http:\/\/app.hindipublic.com\/app_allwhatsupstatus\/menu\/medium\/7611504072164.jpg"}],"sub_menu":"true"},{"app_menu_id":"71","app_menu_name":"Wishes","all_sub_menu":[{"app_menu_id":"71","app_sub_menu_id":"51","app_sub_menu_name":"Good
Morning","app_sub_menu_image":"http:\/\/app.hindipublic.com\/app_allwhatsupstatus\/menu\/medium\/5171504072183.jpg"}],"sub_menu":"true"},{"app_menu_id":"72","app_menu_name":"Among Friend","all_sub_menu":[{"app_menu_id":"72","app_sub_menu_id":"52","app_sub_menu_name":"Friendship","app_sub_menu_image":"http:\/\/app.hindipublic.com\/app_allwhatsupstatus\/menu\/medium\/4411504072205.jpg"}],"sub_menu":"true"},{"app_menu_id":"73","app_menu_name":"Jokes","all_sub_menu":[{"app_menu_id":"73","app_sub_menu_id":"53","app_sub_menu_name":"Santa
Banta","app_sub_menu_image":"http:\/\/app.hindipublic.com\/app_allwhatsupstatus\/menu\/medium\/4331504072225.jpg"}],"sub_menu":"true"},{"app_menu_id":"74","app_menu_name":"Featured","all_sub_menu":[{"app_menu_id":"74","app_sub_menu_id":"54","app_sub_menu_name":"Ganpati
Bappa","app_sub_menu_image":"http:\/\/app.hindipublic.com\/app_allwhatsupstatus\/menu\/medium\/4771504072247.jpg"}],"sub_menu":"true"}]}
Suppose "response" is your JSONResponse
JSONObject jsonObject = new JSONObject(response);// This is used to get jsonObject from response
String sCode=jsonObject.optString("scode"); // This is how you can parse string from jsonObject
JSONArray allmenuArray=jsonObject.optJSONArray("all_menu"); //This is how you can parse JsonArray from jsonObject
for(int i=0;i<allmenuArray.length();i++){
JSONObject objectJson=allmenuArray.optJSONObject(i);//This is how you can parse jsonObject from jsonArray
}
Like This you can parse all your jsonObject and jsonarray. Just follow these steps you can easily parse your full JSONResponse
Try this.
try {
JSONObject jsonObject = new JSONObject("JSONResponse");
String scode = jsonObject.optString("scode");
JSONArray allmenuArray = jsonObject.optJSONArray("all_menu");
for (int i = 0; i < allmenuArray.length(); i++) {
JSONObject objectJson = allmenuArray.optJSONObject(i);
boolean sub_menu = objectJson.getBoolean("sub_menu");
String app_menu_id = objectJson.getString("app_menu_id");
String app_menu_name = objectJson.getString("app_menu_name");
JSONArray all_sub_menu = objectJson.getJSONArray("all_sub_menu");
for (int j = 0; j < all_sub_menu.length(); j++) {
JSONObject data = allmenuArray.optJSONObject(j);
Log.e("app_menu_id", data.getString("app_menu_id"));
Log.e("app_sub_menu_id", data.getString("app_sub_menu_id"));
Log.e("app_sub_menu_name", data.getString("app_sub_menu_name"));
Log.e("app_sub_menu_image", data.getString("app_sub_menu_image"));
}
}
} catch (JSONException e) {
Log.e("ERROr", e.toString());
}

org.json.JSONException: Find specific string from Json Object

I am working in Android and finding json from internet that looks like this:
JSONObject childObject=me.getJSONObject(pos);
String fisrtkey=childObject.getString("A");
JSONArray jsonArray=childObject.getJSONArray("c");
I want to find C21 that is in the A. See the json coming from request.
Can someone help me?
try {
JSONObject j=new JSONObject(data);
JSONArray c= null;
c = j.getJSONArray("This");
JSONObject item=c.getJSONObject(0);
JSONArray me=item.getJSONArray("me");
for(int pos=0;pos<me.length();pos++)
{
JSONObject childObject=me.getJSONObject(pos);
String fisrtkey=childObject.getString("A");
JSONArray jsonArray=childObject.getJSONArray("c");
}
} catch (JSONException e1) {
e1.printStackTrace();
}
//hope this will help you and also check json is invalid or not ,you missed
// bracket of jsonarray "me".
You have missed THIS json array while parsing. First get that and from that json object and then get ME json array. Something like this
JSONObject j = new JSONObject(data);
JSONArray c = j.getJSONArray("This");
JSONObject j1 = c.getJSONArray(0);
JSONArray d = j.getJSONArray("me");
for(int n = 0; n < c.length(); n++) {
JSONObject item = c.getJSONObject(n);
System.out.println(item.getString("A"));
}
Try this:
JSONObject j = new JSONObject(data);
JSONArray c = j.getJSONArray("me");
for(int n = 0; n < c.length(); n++) {
JSONObject person = (JSONObject) c.get(n );
String id = person.getString("A");
...
}

return json encode with array and single value

I want to return this back to my android emulator. This is a combination of jsonArray and jsonObject
[{"id":"11WAD01442","name":"Teng Kwang Wei"},{"id":"11WAD01443","name":"test 1"},{"id":"11WAD01444","name":"test 2"},{"id":"11WAD01445","name":"test 3"},{"status":true}]
What should I use to receive this json encode.
JSONArray jsonArray = new JSONArray(content);
or
JSONObject jsonObject = new JSONObject(content);
You need to decode the top object. In your case is an Array (of JSONObjects):
JSONArray jsonArray = new JSONArray(content);
for (int i=0, len=jsonArray.length(); i<len; i++) try {
JSONObject obj=jsonArray.getJSONObject(i);
String id=obj.getString("id");
String name=obj.getString("name");
.
.
Log.d("DUMP", "User "+i+": id="+id+", name="+name);
} catch (JSONException e) {
Log.d("DUMP", "Problem with user");
}

How do I pull the string array from this json object?

I am trying to get a list of available numbers from the following json object, using the class from org.json
{
"response":true,
"state":1,
"data":
{
"CALLERID":"81101099",
"numbers":
[
"21344111","21772917",
"28511113","29274472",
"29843999","29845591",
"30870001","30870089",
"30870090","30870091"
]
}
}
My first steps were, after receiving the json object from the web service:
jsonObj = new JSONObject(response);
jsonData = jsonObj.optJSONObject("data");
Now, how do I save the string array of numbers?
use:
jsonObj = new JSONObject(response);
jsonData = jsonObj.optJSONObject("data");
JSONArray arrJson = jsonData.getJSONArray("numbers");
String[] arr = new String[arrJson.length()];
for(int i = 0; i < arrJson.length(); i++)
arr[i] = arrJson.getString(i);
you need to use JSONArray to pull data in an array
JSONObject jObj= new JSONObject(your_json_response);
JSONArray array = jObj.getJSONArray("data");
Assuming that you are trying to get it in a javascript block, Try something like this
var arrNumber = jsonData.numbers;
My code is for getting "data":
public void jsonParserArray(String json) {
String [] resultsNumbers = new String[100];
try {
JSONObject jsonObjectGetData = new JSONObject(json);
JSONObject jsonObjectGetNumbers = jsonObjectGetData.optJSONObject("results");
JSONArray jsonArray = jsonObjectGetNumbers.getJSONArray("numbers");
for (int i = 0; i < jsonArray.length(); i++) {
resultsNumbers[i] = jsonArray.getString(i);
}
} catch (JSONException e) {
e.printStackTrace();
Log.e(LOG_TAG, e.toString());
}
}

Categories

Resources