2016-04-30 50 views
0

我工作的一个项目,我想从这个链接提取一些数据此JSON文件(http://www.eurogamer.net/ajax.php?action=json-comments&aid=1822104&start=0&limit=1000&filter=all&order=asc如何获取Key:来自JSON JArray的值对?

一部分是在这里:[{"id":6044586,"u":"ubergine","uid":"472505","a":"2011\/community\/users\/4\/7\/2\/5\/0\/5\/user-472505-originalxoriginal-16-30-27.jpg","t":"3 weeks ago","k":"+16",**"p":"Prepare to Tri"**,"e":"","d":"","v":"unverified","gi":[],"s":"","mod":false,"total":"28"}]

我试过Regex,但没有找到任何好处。然后我用Json.NET使用JArray它解析数据并使用JToken但是无法得到。我正在使用C#编程语言。 我想要p以上键的具体数值在上面?

+0

的JSON是无效的。 [jsonlint.com](http://jsonlint.com) – Utkanos

+1

你使用什么编程语言? –

+0

@Utkanos由OP提供的链接上的json是有效的。 –

回答

1

您可能更适合制作映射到json属性的类并使用JsonConvert.DeserializeObject<>()

看到这个linqpad脚本: http://share.linqpad.net/7fpltw.linq

源在这里(注意,这是一个LinqPad脚本,所以你会得到错误,如果你只是粘贴到Visual Studio):

void Main() 
{ 
string thejson = @"[{ 
    ""id"": 6044586, 
    ""u"": ""ubergine"", 
    ""uid"": ""472505"", 
    ""a"": ""2011\/community\/users\/4\/7\/2\/5\/0\/5\/user-472505-originalxoriginal-16-30-27.jpg"", 
    ""t"": ""4 weeks ago"", 
    ""k"": ""+16"", 
    ""p"": ""Prepare to Tri"", 
    ""e"": """", 
    ""d"": """", 
    ""v"": ""unverified"", 
    ""gi"": [], 
    ""s"": """", 
    ""mod"": false, 
    ""total"": ""28"" 
}, { 
    ""id"": 6044596, 
    ""u"": ""Fragtaster"", 
    ""uid"": ""828120"", 
    ""a"": ""2014\/community\/users\/8\/2\/8\/1\/2\/0\/user-828120-originalxoriginal-8-03-00.jpg"", 
    ""t"": ""4 weeks ago"", 
    ""k"": ""+44"", 
    ""p"": ""This [Dark Souls 3] is one of those cases where <strong>\""more of the same\""<\/strong> is a bloody damn good-thing.<br \/>\n<br \/>\n<img src=\""https:\/\/tse2.mm.bing.net\/th?id=OIP.M607bf0698ed2368b9f9cc9e4ea244c77H0&pid=15.1\"" \/>"", 
    ""e"": """", 
    ""d"": """", 
    ""v"": ""unverified"", 
    ""gi"": [], 
    ""s"": """", 
    ""mod"": false, 
    ""total"": ""66"" 
}]"; 

    var theobj = JsonConvert.DeserializeObject<List<JsonData>>(thejson); 
    theobj.ForEach(o => o.p.Dump()); 
} 

// Define other methods and classes here 
public class JsonData 
{ 
    public string id { get; set; } 
    public string u { get; set; } 
    public string uid { get; set; } 
    public string a { get; set; } 
    public string t { get; set; } 
    public string k { get; set; } 
    public string p { get; set; } 
    public string e { get; set; } 
    public string d { get; set; } 
    public string v { get; set; } 
    public string[] gi { get; set; } 
    public string s { get; set; } 
    public string mod { get; set; } 
    public string total { get; set; } 
} 
+0

这是什么转储()方法?它不是内置的。 –

+0

这是一个[LinqPad的东西](https://www.linqpad.net/)。你可以使用'Console.WriteLine()'来代替。 – Crowcoder

0

我找到了答案上另一个论坛。它可以帮助其他人。 下面是一段代码:

JArray parsedArray = JArray.Parse(jsonString); 
     foreach (JObject parsedObject in parsedArray.Children<JObject>()) 
     { 
      foreach (JProperty parsedProperty in parsedObject.Properties()) 
      { 
       string propertyName = parsedProperty.Name; 
       if (propertyName.Equals("p")) 
       { 
        string propertyValue = (string)parsedProperty.Value; 
        Console.WriteLine("Name: {0}, Value: {1}", propertyName, propertyValue); 
       } 
      } 
     } 

这里是链接,完整代码答案: https://social.msdn.microsoft.com/Forums/vstudio/en-US/df1d6c1b-349a-4f7f-9168-5bf01d320075/how-to-get-keyvalue-pair-from-json-jarray?forum=csharpgeneral