2015-03-03 39 views
0
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返回值:(寧靜)解析與JSONValue.parse Java錯誤JSON

{"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 list = this.GetAllProjectByNameFileExcept(NameFile); 
 
output_json.put("result", list);

我被檢查輸出:

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

但如果列表解析與此代碼:

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) 

代碼在其他功能sucess解析我認爲錯誤,因爲ArrayList字符串類型的數據..我該怎麼辦?

+0

我認爲你的方法有問題。你需要顯示你的方法的代碼「GetAllProjectByNameFileExcept(NameFile);」看看究竟是什麼。 – 2015-03-03 04:00:19

+0

done @NomeshDeSilva。 – Orlando 2015-03-03 04:06:29

+0

將您的json字符串格式化爲將雙引號括在您的數組中,例如[「A11.2011.05900.pdf」,「A11.2011.05930.pdf」,...]。我認爲這是你當前的json字符串無效的問題。檢查我的答案。 – 2015-03-03 04:10:42

回答

0

您的JSON字符串錯誤。你的內部數組「數據」應該是一個字符串數組。意味着你錯過了包裝值的雙/單引號。 檢查使用此link

您正確的JSON輸出應該是這樣的json的有效性:

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

嘗試將其格式化像上面,然後解析它。 你可以在線解析它並檢查鏈接中的有效性: online json parser

+0

謝謝@NomeshDeSilva輸出函數json錯誤,當我檢查有效性,但我如何使格式[「foo」,..,「foo」]。我發現錯誤,因爲輸出json像這樣{「code」:「{\」message \「:\」Success \「}」}所以解析器無法解析。所以我改變我的方法。感謝您的回覆和有效性json信息:D – Orlando 2015-03-03 09:02:19

+0

請發佈您的最新方法,如果我的回覆確實指導了您,請給予一些投票兄弟! – 2015-03-03 10:34:50

+0

@Olando,此行的輸出是「cursor.next()。get(」_ id「)」,並且我建議將您的List聲明更改爲以下內容:列表 list = new ArrayList (); – 2015-03-03 10:40:53