简体   繁体   English

适用于Android的JSON解析器的问题,适用于Java

[英]Issue with JSON parser for android, which works well for java

I have the following json parsing code which works fine when tested as a java application. 我有以下json解析代码,在作为Java应用程序进行测试时可以正常工作。 But on using it with in an android platform and running , returned the following error 但是在Android平台上使用它并运行时,返回以下错误

"Unexpected token END OF FILE at position 0" “位置0处的意外令牌END OF FILE”

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"}] [{{“ user_id”:“ 1”,“ username”:“ arvind”,“ password”:“ somu”,“ firstname”:“ Arvind somu”,“ accountNumber”:“ 1234567”,“ lastname”:“”, “地址”:“”,“电子邮件”:“ sample@gmail.com”}]

I have got the value 1 printed, when tried with java, but no idea why this issue is coming with android. 当我尝试使用Java时,我已经打印了值1,但不知道为什么这个问题与android一起出现。 Can body suggest what is wrong with the code.The parser I am using is json-simple1.1.1 身体可以建议代码有什么问题吗。我正在使用的解析器是json-simple1.1.1

Use this: 用这个:

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

The method optJSONObject returns a JSONObject and you dont have to cast it, where as get() returns an Object. 方法optJSONObject返回一个JSONObject,而不必强制转换它,而get()返回一个Object。 Try this i think this may solve it. 试试这个,我认为这可以解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM