android json object can not be convert to jsonarray? [closed] - android

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I am getting Exception that json object cannot be converted to json array..,when I am showing values from server, here is my code. Please help me.
if(respons!=null){
try{
JSONObject jsonObj=new JSONObject(respons);
JSONArray post=jsonObj.getJSONArray("List of image URLs");
for(int i =0;i<post.length();i++){
String values = post.getString(i);
_issueList.add(values);
}
}
Here is my json exception at this output
{
"message": [
"http:\/\/app.lionforge.com\/comics\/adminpanel\/upload\/1389346961Quincredible_1-
2.png",
"http:\/\/app.lionforge.com\/comics\/adminpanel\/upload\/1389346977Quincredible_1-
3.png",
"http:\/\/app.lionforge.com\/comics\/adminpanel\/upload\/1389346996Quincredible_1-
4.png",
"http:\/\/app.lionforge.com\/comics\/adminpanel\/upload\/1389347016Quincredible_1-
5.png",
"http:\/\/app.lionforge.com\/comics\/adminpanel\/upload\/1389347039Quincredible_1-
6.png",
"http:\/\/app.lionforge.com\/comics\/adminpanel\/upload\/1389347052Quincredible_1-
7.png",
"http:\/\/app.lionforge.com\/comics\/adminpanel\/upload\/1389347062Quincr32.png"
]
}
This data is coming from server. I don't know why this error is occurring, my images are not showing and moving.
Please help me. Thank you.

if(respons!=null){
try{
JSONObject jsonObj=new JSONObject(respons);
JSONArray post=jsonObj.getJSONArray("message");
for(int i =0;i<post.length();i++){
String values = post.getString(i);
_issueList.add(values);
}

change to:
if(respons!=null){
try{
JSONObject jsonObj=new JSONObject(respons);
JSONArray post=jsonObj.getJSONArray("message");
for(int i =0;i<post.length();i++){
String values = post.getString(i);
_issueList.add(values);
}
}
key for your json array is message not List of image URLs

Change your below line in which you need to get the arraylist of message where you have written wrong key. Other code is fine.
JSONArray post=jsonObj.getJSONArray("List of image URLs");
Change it to
JSONArray post=jsonObj.getJSONArray("message");
As you are getting only the JSONArray in your response there is not need of JSONObject in your code.
You can parse your array as below which will directly give you an array :
try{
JSONArray post=new JSONArray(respons);
for(int i =0;i<post.length();i++){
String values = post.getString(i);
_issueList.add(values);
}

Related

How to parse this Json with no object name [duplicate]

This question already has answers here:
How do I parse JSON in Android? [duplicate]
(3 answers)
Closed 5 years ago.
{
"quote": "To understand the heart and mind of a person, look not at
what he has already achieved, but at what he aspires to.",
"author": "Kahlil Gibran (1883-1931)"
}
How to parse this with no any object name
There are different methods to parse json object
Simple way
JSONObject myJson = new JSONObject(yourJsonString);
String quote = myJson. getString("quote");
String author = myJson. getString("author");
Another and recomented way is parse json using Gson
For this you can refer below links
How to parse json parsing Using GSON in android
https://www.journaldev.com/2321/gson-example-tutorial-parse-json
How to do it ?
If you meet {} in your code , you can use JSONObject to parse it .
If you meet [] in your code , you can use JSONArray to parse it .
And if you meet [] in your code , you can use for loop to get value in it .
And you should use try catch in your code .
Try this .
try {
JSONObject jsonObject = new JSONObject(response);
String author = jsonObject.getString("author");
String quote = jsonObject.getString("quote");
} catch (JSONException e) {
e.printStackTrace();
}
This is absolutely normal json.
JSONObject json = new JSONObject(your_json);
String quote = json.getString("quote");
String author = json.getString("author");

JSONObject cannot be converted to JSONArray when the return is array [duplicate]

This question already has answers here:
Difference between JSONObject and JSONArray
(9 answers)
Closed 6 years ago.
it is showing me the following error:
JSONObject cannot be converted to JSONArray
Here is my code:
JSONArray jsonArray = jsonObject.getJSONArray("GetTabDataResult");
My JSON Result:
{"GetTabDataResult":{"TabId":1,"Bands":[{"Id":1,"ImageURL":"http:\/\/www.yallaasouq.com\/bands\/1.jpg","BrandId":0,"Men":true,"Women":true,"Kids":true,"Accessories":true,"Disabled":false,"Title":"","Type":0,"TypeId":0},{"Id":2,"ImageURL":"http:\/\/www.yallaasouq.com\/bands\/2.jpg","BrandId":0,"Men":true,"Women":true,"Kids":true,"Accessories":true,"Disabled":false,"Title":"","Type":0,"TypeId":0},{"Id":3,"ImageURL":"http:\/\/www.yallaasouq.com\/bands\/3.jpg","BrandId":0,"Men":true,"Women":true,"Kids":true,"Accessories":true,"Disabled":false,"Title":"","Type":0,"TypeId":0},{"Id":4,"ImageURL":"http:\/\/www.yallaasouq.com\/bands\/4.jpg","BrandId":0,"Men":true,"Women":true,"Kids":true,"Accessories":true,"Disabled":false,"Title":"","Type":0,"TypeId":0}],"Brands":[{"Id":4,"ArName":"Fabillo","EnName":"Fabillo","Code":"","Description":"","ImageURL":"http:\/\/www.yallaasouq.com\/brands\/fabillo.jpg","Men":true,"Women":false,"Kids":false,"Accessories":false,"FacebookLink":"","InstagrmLink":"","Disabled":false},{"Id":7,"ArName":"Carolina Boix","EnName":"Carolina Boix","Code":"","Description":"","ImageURL":"http:\/\/www.yallaasouq.com\/brands\/eva.jpg","Men":true,"Women":true,"Kids":false,"Accessories":false,"FacebookLink":"","InstagrmLink":"","Disabled":false},{"Id":9,"ArName":"Sport Master","EnName":"Sport Master","Code":"","Description":"","ImageURL":"http:\/\/www.yallaasouq.com\/brands\/sportmaster.jpg","Men":true,"Women":true,"Kids":true,"Accessories":false,"FacebookLink":"","InstagrmLink":"","Disabled":false},{"Id":10,"ArName":"Bershka","EnName":"Bershka","Code":"","Description":"","ImageURL":"http:\/\/www.yallaasouq.com\/brands\/eva.jpg","Men":true,"Women":true,"Kids":true,"Accessories":true,"FacebookLink":"","InstagrmLink":"","Disabled":false},{"Id":11,"ArName":"NO EXCESS","EnName":"NO EXCESS","Code":"","Description":"","ImageURL":"http:\/\/www.yallaasouq.com\/brands\/noexcess.jpg","Men":true,"Women":true,"Kids":true,"Accessories":true,"FacebookLink":"","InstagrmLink":"","Disabled":false}],"Categories":[{"Id":8,"EnName":"T-Shirts","ArName":"بلوزات","Code":"8","Men":true,"Women":true,"Kids":true,"Accessories":false,"MenImgURL":"http:\/\/www.yallaasouq.com\/Categories\/tshirt01.jpg","WomenImgURL":"","KidsImgURL":"","AccessoriesImgURL":""},{"Id":14,"EnName":"Shoes","ArName":"احذية","Code":"14","Men":true,"Women":true,"Kids":true,"Accessories":false,"MenImgURL":"http:\/\/www.yallaasouq.com\/Categories\/shoes01.jpg","WomenImgURL":"","KidsImgURL":"","AccessoriesImgURL":""},{"Id":17,"EnName":"Accessories","ArName":"اكسسوارت","Code":"17","Men":true,"Women":true,"Kids":false,"Accessories":false,"MenImgURL":"http:\/\/www.yallaasouq.com\/Categories\/accessories01.jpg","WomenImgURL":"","KidsImgURL":"","AccessoriesImgURL":""},{"Id":6833,"EnName":"Pants","ArName":"بنطلونات","Code":"6833","Men":true,"Women":true,"Kids":true,"Accessories":false,"MenImgURL":"http:\/\/www.yallaasouq.com\/Categories\/pants01.jpg","WomenImgURL":"","KidsImgURL":"","AccessoriesImgURL":""},{"Id":6834,"EnName":"Shirts","ArName":"قمصان","Code":"6834","Men":true,"Women":true,"Kids":true,"Accessories":false,"MenImgURL":"http:\/\/www.yallaasouq.com\/Categories\/hoodie01.jpg","WomenImgURL":"","KidsImgURL":"","AccessoriesImgURL":""}]}}
I know that my json has sub json objects (Bands, Brands, Categories) but in main object (GetTabDataResult) there is (TabId) which is a value not a sub object.
I also checked the service result in the fiddler and i attached here a photo of the screen...you can check it.
use
JSONObject jsonOb = jsonObject.getJSONObject("GetTabDataResult");
instead of
JSONArray jsonArray = jsonObject.getJSONArray("GetTabDataResult");
this is jsonarray
Bands, Brands, Categories
JsonArray bands =jsonOb.getJsonArray("Bands");
Hope above code is helpful to you !
I see no JsonArray in the Json String you provided, this is a single json object. Json Arrays are surronded by [ ].
So instead convert it to a normal JSONObject:
jsonObject.getJSONObject("GetTabDataResult");

How to parse data by array in android? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am developing an android application were I want to parse data through an array to URL API.
Taking an example, there are 5 TextBox and I enter some information in it. Then all the values entered in textView should parse in an array format to That API URL.
Please help!!
This is what i did, note that this is just an example.
final Map<String,String> postParam = new HashMap<String, String>();
for(int i = 0; i < 5; i++)
postParam.put("child_id[" + i + "]", i+"");
You will get :
child_id[0] with value 0
child_id[1] with value i
And goes on.
UPDATE
In your case, you might want to do something like :
postParam.put("child_id[" + i + "]", myEditText.getText.toString());
for each of your edittext.
Feel free to comment if you dont understand my answer or if i miss-understood you.
I hope i am understanding you correctly, you want to put 5 TextBox's entered text into one Array and then send this Array to API.
Try this:
ArrayList<String> textViewTexts = new ArrayList<String>();
// Put all EditText's text to array
// Do this for each EditText
textViewTexts.add(someEditText.getText());
You can then use textViewTexts.toString() and send this to API.
EDIT:
you can parse textViewTexts like this:
for (int i = 0; i < textViewTexts.size(); i++) {
String text = textViewTexts.get(i);
// Do something with text..
}
EDIT2:
you can parse textViewTexts like this:
JSONArray jArray=new JSONArray();
for (int i = 0; i < textViewTexts.size(); i++) {
String text = textViewTexts.get(i);
jArray.put(text);
}
// Send JSONArray to API
jArray.toString();
If you want to send that array to server via an api, then you should send data in JSONArray like this
JSONArray jArray=new JSONArray();
jArray.put(yourTextViewText1);
jArray.put(yourTextViewText2);
jArray.put(yourTextViewText3);
jArray.put(yourTextViewText4);
jArray.put(yourTextViewText5);
and you can send that to server like that
params.put("key",jArray.toString());
Moreover, It is easy for you web developer to parse this JSONArray.

How to show json array object in textview [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How to add json array in textview? I want to show json array object "name" in text view. How do I do it? How to getting single element inside array? I want to print json array obect name in textview
JSONObject json2 = new JSONObject(str);
status = json2.getString("status");
if (status.equals("1")) {
JSONArray school2 = json2.getJSONArray("data");
txt1.setText(data2.getString("name"));
}
"status":1,
"data":
[
{
"dish_id":"37",
"name":"dish2",
"description":"testing",
"allergen":
"Eggs3,walnuts",
"image":"http:\/\/198.57.208.46\/~school\/uploads\/images\/dishes\/egg-biryani.jpg",
"ingredient":"weqweqw23,dsfds"
}
],
"dish_nutrition":
{"1":
{
"name":"Cholesterol and Diet",
"qty":"2"
},"2":
{
"name":"Cholesterol and Diet",
"qty":"1"
}
}
}
You can access the field like this:
school2.getJSONObject(0).getString("name")
For the details look it up in the documentation

how to generate JsonStringer for this Json Data Format?

hello i have a JSON data format can anyone please help me to make dynamic JSONStringer object for this String
{"Text":"Hello Simple Text",
"Files":[{"ContentType":"image/png",
"Content":"iVBORw0KGgoAAAANSUhEUgAAAR8AAACMCAIAAADKsDpDAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAB3RJTUUH2wYWDzIB3zSYdQAAAAd0RVh0QXV0aG9yAKmuzEgAAAAMdEVYdERlc2NyaXB0aW9uABMJISMAAAAKdEVYdENvcHlyaWdodACsD8w6AAAADnRFWHRDcmVhdGlvbiB0aW1lADX3DwkAAAAJdEVYdFNvZnR3YXJlAF1w/zoAAAALdEVYdERpc2NsYWltZXIAt8C0jwAAAAh0RVh0V2FybmluZwDAG+aHAAAAB3RFWHRTb3VyY2UA9f+D6wAAAAh0RVh0Q29tbWVudAD2zJa/AAAABnRFWHRUaXRsZQCo7tInAAABAElEQVR4nO2de1zUVf7/3+dzmwsMoCgDXgARBO/"}],
"AuthToken":"XkWQRd65+H+iPtlOoAEYAR0jrzB1o3UV"}
i have used
jsonstr = new JSONStringer().object().key("Text")
.value(msg).key("Files").array().object().key(
"ContentType").value("image/png").key(
"Content").value(enimg)
.endObject().endArray().key("AuthToken").value(token)
.endObject();
but the server is giving me fault message in return, not accepting the data.
actually i was doing the right thing..everything was OK..
the problem was with org.json package it was not accurate with Base64 string
i switched to another library and all worked..
https://stackoverflow.com/questions/338586/a-better-java-json-library
see the above question for another json libraries
that was problem with org.json
i switched to another..and all it works
nesting too deep in JSON... should I switch to XML?
This is a way to do what you want:
// Creating root JSONObject
JSONObject json = new JSONObject();
// Put in it a String field
json.put("Text", "Hello sample");
// Creating a JSONArray
JSONArray arr = new JSONArray();
//Creating the element to populate the array
JSONObject element = new JSONObject();
element.put("ContentType","image/png");
element.put("Content","iVBORw0K...gDXgARBO/");
// Put it in the array
arr.put(element);
// Put the array and other fileds in the root JSONObject
json.put("Files", arr);
json.put("AuthToken", "XkWQ...o3UV");
// Get the JSON String
String s = json.toString();
// Get formatted and indented JSON String
String s2 = json.toString(4);
// 4 is the number of spaces to indent the string
You can use JSONObject class for this purpose
http://developer.android.com/reference/org/json/JSONObject.html

Categories

Resources