1

I got an asp.net WCF which contain a function that will serialize the LINQ results and return me this json string

"[{\"intPromoter\":1,\"varID\":\"DP900001\",\"varName\":\"Jay Chou\"}]"

But it seem like a bit different from what i'm learn json so i added "rows" with this code in android java

  text = "{\"rows\":" + text  + "}";

and it become like this

{"rows":"[{\"intPromoter\":1,\"varID\":\"DP900001\",\"varName\":\"Jay Chou\"}]"}

so when i use the below function to parse into json array it's give me java.lang.string cannot converted to jsonarray. So I'm not sure is string from .net need to change into certain format before read by json or is there a direct way without add "rows" at the beginning. Thanks in advance.

JSONObject jObject = new JSONObject(results);
JSONArray jArray = jObject.getJSONArray("rows"); //This LINE GIVE error,

for (int i=0; i < jArray.length(); i++)
{
JSONObject oneObject = jArray.getJSONObject(i);

int intPromoter =Integer.parseInt(oneObject.getString("KEY_intPromoter"));
String varID = oneObject.getString("KEY_varID");
String varName = oneObject.getString("KEY_varName");
}

1 Answer 1

2

Your final string has the array enclosed in double quote marks, which means it's a string not an array. Remove these and it should work OK.

E.g.

{"rows":[{"intPromoter":1,"varID":"DP900001","varName":"Jay Chou"}]}

Also, you should be able to remove the need for a JSONObject entirely by instantiating a JSONArray from the string from the server directly, e.g.

JSONArray rows = new JSONArray(json);

See the following documentation for more info:

http://developer.android.com/reference/org/json/JSONArray.html

Sign up to request clarification or add additional context in comments.

1 Comment

Thx for helps. just for who are interested on this piece when json string return from asp.net by serializer. I'm not sure why it return "not" standard json. so i neeed to replace this string in the android java to make it work. text = text.replace("\\\"", "\""); text = text.replace("\"[", "["); text = text.replace("]\"", "]");

Your Answer

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.