如何从JSON JArray获取Key:Value对?

时间:2016-04-30 12:14:14

标签: c# json json.net

我正在开展一个项目,我想从这个链接中提取一些数据(http://www.eurogamer.net/ajax.php?action=json-comments&aid=1822104&start=0&limit=1000&filter=all&order=asc

这个json文件的一部分在这里:[{"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键的具体值为粗体?

2 个答案:

答案 0 :(得分:1)

您可能最好创建一个映射到json属性并使用JsonConvert.DeserializeObject<>()的类。

请参阅此linqpad脚本: http://share.linqpad.net/7fpltw.linq

source here(注意这是一个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; }
}

答案 1 :(得分:1)

我在另一个论坛上找到了答案。它可能会帮助别人。 这是一段代码:

    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