-2

I have a little question about parsing a JSON string in Android. I'm trying to parse this JSON :

{
   "error":504,
   "result":"[\"USRL3C4NAEL84727\",\"USR1HZE4UN5289H1\",\"USR46476WQB92M84\",\"USR4ZQ193YGRK782\"]"
}

The part that I need actually is to parse the result. I did try this :

JSONObject jObj = new JSONObject(responseString);
            int error = Integer.parseInt(jObj.optString("error","0"));
            String errorMessage = jObj.optString("result","");
            if(error==504){
                JSONArray savedCodes = jObj.getJSONArray("result");
                for (int i = 0; i < savedCodes.length(); i++) {
                    String returnString = savedCodes.getString(i);
                    Log.e("","returnString : "+returnString);
                }
            }

But it's throwing me an exception :

org.json.JSONException: Value ["USR9BA13IZG27H93","USRI818369RH919Y","USRWT7612CQV4375","USR7739R644X84A1"] at result of type java.lang.String cannot be converted to JSONArray

So I guess it's not JSONArray and I need to know how to parse the result part and get this at the end :

String 1 = USR9BA13IZG27H93
String 2 = USRI818369RH919Y
and etc...

Any solutions?

Android-Droid
  • 14,365
  • 41
  • 114
  • 185
  • 1
    possible duplicate of [How can I parse this JSON in Android?](http://stackoverflow.com/questions/3605077/how-can-i-parse-this-json-in-android) and about a million other questions on StackOverflow. – Polynomial Dec 07 '11 at 12:01
  • Go to google and search for "Android JSON Parsing tutorial". – Paresh Mayani Dec 07 '11 at 12:03
  • I know it is a possible duplicate,but I ask that question because in some situations I can receive that as a `result` and I need to devide the string between `""` so I can insert them into Database – Android-Droid Dec 07 '11 at 12:07
  • Quickly edit your question with your doubt fully. – Paresh Mayani Dec 07 '11 at 12:20

3 Answers3

2

Perhaps this Android JSON parsing tutorial will be useful: http://www.androidcompetencycenter.com/2009/10/json-parsing-in-android/

nwaltham
  • 2,067
  • 1
  • 22
  • 40
2

there is a tutorial here on it

http://p-xr.com/android-tutorial-how-to-parse-read-json-data-into-a-android-listview/

0

The easiest way to do this :

String errorMessage = jObj.optString("result","");
if(error==504){
     String first = errorMessage.replace("[", "");
     String second = first.replace("]", "");
     String third = second.replace("\"", "");
     String[] split = third.split(",");

     for(int i=0;i< split.length;i++)
         Log.e("","SPLIT : "+split[i]);
}
Android-Droid
  • 14,365
  • 41
  • 114
  • 185