解析json数据并打印结果



解析json数据并打印结果:

JSON提要:

{
    "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"
        }
    ]
}
下面是解析的初始代码:
dynamic outputArray = JsonConvert.DeserializeObject(json);
dynamic Data = outputArray.data;
foreach (var jsonDataVal in Data)
{
    s = s + "url: " + jsonDataVal.url;
    s = s + "source: " + jsonDataVal.source;
}

我需要得到作者数据,但无法得到它。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;
    //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.name, author.link, author.picture中获取数据?

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

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;
}

注意,为了将JValue转换为String,需要.ToString()

您可以放弃dynamic并使用JSON中的类型。净。此外,您还可以编写递归调用的方法来获取给定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;
}

在你的例子中,上面会吐出以下内容:

的url: 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

相关内容

  • 没有找到相关文章