解析json数据并打印结果

时间:2015-09-02 03:38:31

标签: c# json json.net

解析json数据并打印结果:

JSON FEED:

Incorrect number of arguments

以下是解析的初始代码:

{
    "data": [   
        {  
            "url": "http://test.com",
            "source": 2,
            "created": 144139828328,
            "user_id": 1238,
            "visible": 1,
            "comments": "hello",
            "author": "{\"name\":\"johndoe\",\"link\":\"http://url.com/johndoe\",\"picture\":\"//url.com/imageurl.jpg\",\"username\":\"johndoe21\",\"fullname\":\"John Doe\"}",
            "post_id": "user_1238"
        }
    ]
}

我需要获取作者数据,但无法获取它... jsonDataVal.author.name无效。

然后我这样做了:

dynamic outputArray = JsonConvert.DeserializeObject(json);
dynamic Data = outputArray.data;
foreach (var jsonDataVal in Data)
{
    s = s + "url: " + jsonDataVal.url;
    s = s + "source: " + jsonDataVal.source;
}

如何从作者字段dynamic outputArray = JsonConvert.DeserializeObject(json); dynamic Data = outputArray.data; foreach (var jsonDataVal in Data) { s = s + "url: " + jsonDataVal.url; s = s + "source: " + jsonDataVal.source; //obtained author data into string and applied JsonConvert.DeserializeObject string author = Data.author dynamic outputArray = JsonConvert.DeserializeObject(author); var authdata = JsonConvert.DeserializeObject(author); s = s + "author name: " + authdata.name; //not working } author.nameauthor.link获取数据?

2 个答案:

答案 0 :(得分:1)

实际上,author对象不是数组。您解析它两次,因为它是一个存储为字符串的简单JSON对象。此外,在您的JSON示例中,作者属于数组项;但是,出于某种原因,您尝试从Data对象中提取if:

string author = Data.author; // not jsonDataVal.author

对我来说效果很好:

dynamic outputArray = JsonConvert.DeserializeObject(json);
dynamic Data = outputArray.data;
foreach (var jsonDataVal in Data)
{
    s = s + "url: " + jsonDataVal.url;
    s = s + "source: " + jsonDataVal.source;

    //obtained author data into string and applied JsonConvert.DeserializeObject
    dynamic authorData = JsonConvert.DeserializeObject(jsonDataVal.author.ToString());

    s = s + "author name: " + authorData.name;
}

请注意,要.ToString()转换为JValue,才需要String

答案 1 :(得分:0)

您可以放弃dynamic并使用JSON.NET中的类型。 此外,您可以编写一个递归调用的方法来获取给定JSON对象的所有属性。

// your "main" method
private void Parse(string json) // <- pass in your JSON as string
{
    string properties = string.Empty;

    // parse JSON onbject
    JObject jsonObject = JObject.Parse(json);
    // get an array, which is token called "data"
    JArray data = jsonObject.SelectToken("data").Value<JArray>();

    // iterate over all tokens in data array (in this case just one)
    foreach (JToken token in data.Children())
    {
        // get names and values of all properties within current token 
        foreach (JProperty property in token.Children<JProperty>())
            properties += GetProperty(property);
    }

    // print out results
    Console.WriteLine(properties);
}
// method extracting property name & value
private string GetProperty(JProperty property, string prefix = null)
{
    string value = string.Empty;

    try
    {
        // if property value is another object, call method recursively
        var jsonObject = JObject.Parse(property.Value.ToString());
        foreach (JProperty innerProperty in jsonObject.Children<JProperty>())
            value += GetProperty(innerProperty, property.Name);  

    }
    catch (JsonReaderException)
    {
        // else just format return value
        value = string.Format(@"{0}:{1}{2}", 
            prefix != null ? string.Format("{0} {1}", prefix, property.Name) : property.Name, 
            property.Value, 
            Environment.NewLine);
    }
    return value;
}

在你的情况下,上面会吐出以下内容。

  

网址:http://test.com
  源:2
  创建:144139828328
  USER_ID:1238
  可见:1
  意见:你好
  作者姓名:johndoe
  作者链接:http://url.com/johndoe
  作者图片://url.com/imageurl.jpg
  作者用户名:johndoe21
  作者姓名:John Doe
  POST_ID:user_1238