(restful)使用JSONValue.parse java错误解析json

时间:2015-03-03 03:19:57

标签: java json

public String GetAllProjectByNameFileExcept(String NameFile)
    {
        JSONObject output_json = new JSONObject();
        ArrayList<String> List = new ArrayList<String>();
        DB db = null;
        try 
        {
            db = MONGODB.GetMongoDB();
            DBCollection collLocal = db.getCollection("local");
            BasicDBObject objek_db = new BasicDBObject();
            BasicDBObject objek_db2 = new BasicDBObject();
            objek_db.put("$ne", NameFile);
            objek_db2.put("_id",objek_db);
            DBCursor cursor = collLocal.find(objek_db2);
            while (cursor.hasNext()) {
                List.add(cursor.next().get("_id").toString());
            }

            if (List.size() == 0)
            {
                output_json.put("code", 0);
                output_json.put("message", "Not Found");
                output_json.put("data", null);
            }else
            {
                output_json.put("code", 1);
                output_json.put("message", "Success");
                output_json.put("data", List);
            }
        } 
        catch (Exception e) 
        {
            output_json.put("code", -1);
            output_json.put("message",e.toString());
        }

        return output_json.toString();  
    }

我有函数GetAllProjectByNameFileExcept返回值:

{"code":1,"data":[A11.2011.05900.pdf, A11.2011.05930.pdf, A11.2011.05931.pdf, A11.2011.05932.pdf],"message":"Success"}

类型数据[A11.2011.05900.pdf,...,n.pdf]是ArrayList String
我在其他函数中调用了这个函数:

&#13;
&#13;
String list = this.GetAllProjectByNameFileExcept(NameFile);
output_json.put("result", list);
&#13;
&#13;
&#13;

我被检查输出:

{"result":"{\"code\":1,\"data\":[A11.2011.05900.pdf, A11.2011.05930.pdf, A11.2011.05931.pdf, A11.2011.05932.pdf],\"message\":\"Success\"}"}

但如果 list 使用此代码进行解析:

JSONObject FileData = (JSONObject) JSONValue.parse(list);

总是收到错误

java.lang.Error: Error: could not match input
org.json.simple.JSONValue.parse(Unknown Source)
service.CrawLocal.PlagiarismCheck(CrawLocal.java:276)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

代码解析其他函数成功我认为错误因为ArrayList字符串类型数据..我该怎么办?

1 个答案:

答案 0 :(得分:0)

您的JSON字符串错误。你的内部阵列&#34;数据&#34;应该是一个字符串数组。意味着你错过了包装值的双/单引号。 使用此link

检查json有效性

您正确的JSON输出应如下所示:

&#13;
&#13;
{
  "code": 1,
  "data": [
    "A11.2011.05900.pdf",
    "A11.2011.05930.pdf",
    "A11.2011.05931.pdf",
    "A11.2011.05932.pdf"
  ],
  "message": "Success"
}
&#13;
&#13;
&#13;

尝试像上面那样格式化它,然后解析它。 您可以在线解析并检查此链接的有效性: online json parser