2015-08-18 46 views
0

我使用的是西風API和它的返回我包含像這樣的JSON對象JSON數組...如何僅讀取json響應中的一個值?

{ 
"executions": [ 
    { 
     "id": 24, 
     "cycleId": 13, 
     "cycleName": "CLONE - Audit Test Cycle 2", 
     "issueKey": "ZFJ-2", 
     "projectKey": "ZFJ", 
     "project": "ZFJ", 
     "priority": "Major", 
     "component": "c1", 
     "versionId": 10000, 
     "version": "v1", 
     "status": { 
      "id": 2, 
      "name": "FAIL", 
      "description": "Test was executed and failed.", 
      "color": "#CC3300", 
      "type": 0 
     }, 
     "executedOn": "18/Oct/13", 
     "creationDate": "18/Oct/13", 
     "executedBy": "admin", 
     "executedByUserName": "admin" 
    }, 
    {...}, 
    { 
     "id": 1, 
     "cycleId": 1, 
     "cycleName": "Audit Test Cycle", 
     "issueKey": "ZFJ-2", 
     "projectKey": "ZFJ", 
     "project": "ZFJ", 
     "priority": "Major", 
     "component": "c1", 
     "versionId": 10000, 
     "version": "v1", 
     "status": { 
      "id": 1, 
      "name": "PASS", 
      "description": "Test was executed and passed successfully.", 
      "color": "#75B000", 
      "type": 0 
     }, 
     "executedOn": "4/Oct/13", 
     "creationDate": "4/Oct/13", 
     "executedBy": "admin", 
     "executedByUserName": "admin" 
    } 
] 

本來我是用org.json解析JSON數組,但它似乎很慢,我當我只解析約200個json對象時。我需要從每個對象中唯一需要的是「id」,所以我想通過掃描每個json對象中的「id」來解析文檔,而不是使用org.json來解析整個json響應。

這會提高我的解析性能而不是使用org.json嗎?如何在沒有解析器的情況下掃描json響應?我假設json響應仍然是一行文本,解析器是檢索鍵和值的東西。

是否有更好的選擇我所建議的?更高效的解析器? org.json的性能與其他解析器相比如何?

+0

你不需要自己寫這個,它已經存在。看看這個例子http://www.ngdata.com/parsing-a-large-json-file-efficiently-and-easily/ – Dici

回答

1

手動解析JSON並不是一個好主意。

只要去建立一個圖書館,不會有問題。 Jackson可能是一個不錯的選擇。它表現出良好的表現,並且鏈接的例子很容易遵循。如果您只需要ID字段(您可以放心地省略其餘字段),則不需要創建複雜的結構。