0

first of all, I've read a ton of similar questions on this site and none has solved my problem so
I have this json file that I create using php :

{
"done":true,
"data":"[
{\"id\":\"5099\",\"user_id\":\"892\"},
{\"id\":\"5100\",\"user_id\":\"892\"}
....
]"}

now in java android I want to parse it but it gives me this error: JSON.TypeMismatch
this is my android code to parse the json file:

//class from which I get the json in a JSONObject - works fine so far
JSONObject httpResult = itemHttpRequest.get("/fetch?currentList=&sort=recent&extra=");
 try {
 JSONArray httpData = httpResult.getJSONArray("data");
 } catch (JSONException e) {
 e.printStackTrace();
 }

but it keeps giving me error.
is it because the data array in my json file is surrounded by quotes?
please help

asked Dec 18, 2014 at 8:52

5 Answers 5

4

For sure, data is a String (character surrounded by quotes), it can't be read as a JSONArray.

If you can't make your php to generate a JSON array, what you can do on Android size, is to get the data string with :

String data = httpResult.getString("data");

then you can create a JSONARRAY with :

JSONArray constructor

JSONArray dataArray = new JSONArray(data)
answered Dec 18, 2014 at 8:54
Sign up to request clarification or add additional context in comments.

1 Comment

you sir are a life savor, I wish I could kiss you or something :*
2
"data":"[
{\"id\":\"5099\",\"user_id\":\"892\"},
{\"id\":\"5100\",\"user_id\":\"892\"}
....
]"

is a string

But

"data":[
{"id":"5099","user_id":"892"},
{"id":"5100","user_id":"892"}
....
]

is array

answered Dec 18, 2014 at 8:54

1 Comment

@user3425760 where are you from take this json?
2

You are supplying invalid JSON. You need to correct your JSON by removing extra quotes. You can always use some validator to spot these types of problem E.g.

{
 "done": true,
 "data": [
 {
 "id": "5099",
 "user_id": "892"
 },
 {
 "id": "5100",
 "user_id": "892"
 }
 ]
}
answered Dec 18, 2014 at 8:56

1 Comment

yes, i deleted my comment, but the invalid JSON can also comes from \n in the data String
1

Use this to convert JSON array to string

private String convertStreamToString(InputStream is) {
 /*
 * To convert the InputStream to String we use the
 * BufferedReader.readLine() method. We iterate until the
 * BufferedReader return null which means there's no more data to
 * read. Each line will appended to a StringBuilder and returned as
 * String.
 */
 BufferedReader reader = new BufferedReader(
 new InputStreamReader(is));
 StringBuilder sb = new StringBuilder();
 String line = null;
 try {
 while ((line = reader.readLine()) != null) {
 sb.append(line + "\n");
 }
 } catch (IOException e) {
 e.printStackTrace();
 } finally {
 try {
 is.close();
 } catch (IOException e) {
 e.printStackTrace();
 }
 }
 return sb.toString();
 }
answered Dec 18, 2014 at 12:25

Comments

0

Try with this

 JsonArray array = new JsonArray(value)

value :- Should have proper jsonarray format. otherwise it will throw a JsonException

answered Dec 18, 2014 at 9:32

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.