2014-12-02 67 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); 
} 

希望它可以帮助!