0

I have the following json parsing code which works fine when tested as a java application. But on using it with in an android platform and running , returned the following error

"Unexpected token END OF FILE at position 0"

Here is my code

public boolean parseJSON(String content)  {

    boolean retvalue=false;
    String jsonString=null;
    JSONParser parser=new JSONParser();
    Object obj;
    try {
        System.out.println("in the parse json");
        obj = parser.parse(content);
        JSONArray array=(JSONArray)obj;
        JSONObject obj2=(JSONObject)array.get(0);
        jsonString=(String) obj2.get("user_id");
        System.out.println("in the parse json  "+jsonString);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


    if (null != jsonString) {
        retvalue = true;
    }
    return retvalue;

}

The input string for the method is the following

[{"user_id":"1","username":"arvind","password":"somu","firstname":"Arvind somu","accountNumber":"1234567","lastname":"","address":"","email":"sample@gmail.com"}]

I have got the value 1 printed, when tried with java, but no idea why this issue is coming with android. Can body suggest what is wrong with the code.The parser I am using is json-simple1.1.1

4

1 回答 1

0

用这个:

   JSONObject obj2;
   obj2 = array.optJSONObject(0);

optJSONObject 方法返回一个 JSONObject,您不必强制转换它,而 get() 返回一个 Object。试试这个,我认为这可以解决它。

于 2013-09-05T18:58:27.907 回答