2014-12-02 71 views
0

的URL具有以下JSON數據:這是有效的json數據嗎?

[{ "topic": "cricket", 
    "value": "Player [ playerid=123, category=b, high=150, total=2300]", 
    "place": "xyz"}, 
{ "topic": "cricket", 
    "value": "Player [ playerid=456, category=c, high=60, total=300]", 
    "place": "abc"}, 
{ "topic": "cricket", 
    "value": "Player [ playerid=789, category=a, high=178, total=5300]", 
    "place": "bnm"}] 

我在網上想查這是否是有效的JSON或不通過以下鏈接:http://jsonformatter.curiousconcept.com/它說有效。如果是,我如何訪問每個playerid?

+1

它是有效的,但'playerid'不是你可用的字段。你有'價值','主題'和'地點'。 – 2014-12-02 18:47:42

+0

這是一個有效的字符串。但是,'Player [playerid = 123,category = b,high = 150,total = 2300]'不是一個有效的JSON表達式,它具有可通過編程訪問的'playerid'屬性 - 您需要解析它。 – Bergi 2014-12-02 18:48:02

+0

可能有效,但絕對奇怪。 Player看起來像是一組未正確編碼的對象。 – 2014-12-02 18:48:15

回答

3

它是有效的JSON,但有關播放器的數據嵌入在隨機字符串中。你可以做兩件事情之一:

  1. 更新服務發送回不同的,有效的JS值,例如:

    "value": { 
        "type": "Player", 
        "playerid": 123, 
        "category": "b", 
        "high": 150, 
        "total": 2300 
    } 
    
  2. 解析在value數據鍵自己:

    // Simple regex, not really "parsing" 
    var playerIdRE = /playerid=(\d+)/i; 
    var result = playerIdRE.exec(yourData[0].value); 
    // result[0] is the full match, while result[1] is the ID. 
    
    // Or the more complicated version that does full parsing 
    var format = /\s*(.*?)\s*\[\s*([^\]]+)\s*\]\s*/gi, 
        keyValuePair = /(\w+)=([^,\]]*),?\s*/gi 
    function parseComplexDataType(input) { 
        var result = format.exec(input), 
        typeName = result[1], 
        keyValues = result[2], 
        returnValue = {}; 
        if (!typeName) return returnValue; 
        returnValue.typeName = typeName; 
        input.replace(keyValuePair, function(_, key, value) { 
        returnValue[key] = value; 
        }); 
        return returnValue; 
    } 
    
    // Usage: 
    > parseComplexDataType("Player [ playerid=123, category=b, high=150, total=2300]") 
    Object {typeName: "Player", playerid: "123", category: "b", high: "150", total: "2300"} 
    
+0

所以最後,是不是可以得到playerid?(如果數據是你所提到的形式,我可以自己完成) – user2515138 2014-12-02 19:00:21

+0

@ user2515138 - 有可能,你只需要使用第二個版本,或者更多實際解析完整數據的複雜版本。 – 2014-12-02 19:07:12

0

爲了您的目的,它是無效的。一旦JSON得到糾正,您只需循環遍歷數組並讀取每個值。

var jArray = [{ 
    "topic": "cricket", 
    "value": { 
    "type": "Player", 
    "playerid": 123, 
    "category": "b", 
    "high": 150, 
    "total": 2300 
    }, 
    "place": "xyz" 
}, { 
... 
}] 

要訪問JSON數據...

for (var i=0,len=jArray.length; i<len; i++) { 
    console.log(jArray[i].topic, jArray[i].value.type); 
} 
+0

我同意在這種情況下有效性是形式。 – dandavis 2014-12-02 19:08:53

0

是的,它是。 http://jsonlint.com/

提取 「playerid」::我通過檢查

  1. 初始化字符串來JSONArray。
  2. 迭代上面數組中的每個元素。
  3. 現在,從每個元素中提取「值」。
  4. 最後,從這個字符串中你可以通過使用字符串方法來獲得「playerid」(見下面的代碼)。

下面是Java代碼:

ArrayList<String> player_ids = new ArrayList<String>(); 
String s = "YOUR STRING"; 
JSONArray ja = new JSONArray(s); 
for(int i =0; i<ja.length(); i++) 
{ 
    String value = ja.getJSONObject(i).getString("value"); 
    int start = value.indexOf("="); 
    int end = value.indexOf(","); 
    String player_id = value.substring(start+1, end); 
    player_ids.add(player_id); 
} 

希望它可以幫助!