解析JSON Facebook的C#JSON、Facebook

2023-09-06 17:11:24 作者:终点也是起点

我想了很多时间来分析一个JsonArray,我已经通过graph.facebook了,让我可以额外价值。我想提取的值是消息和ID。

I am trying for many hours to parse a JsonArray, I have got by graph.facebook, so that i can extra values. The values I want to extract are message and ID.

获取JasonArry是没有问题的,并能正常工作:

Getting the JasonArry is no Problem and works fine:

[
    {
    "code":200,
    "headers":[{"name":"Access-Control-Allow-Origin","value":"*"}],
    "body":"{
        \"id\":\"255572697884115_1\",
        \"from\":{
            \"name\":\"xyzk\",
            \"id\":\"59788447049\"},
        \"message\":\"This is the first message\",
        \"created_time\":\"2011-11-04T21:32:50+0000\"}"},
    {
    "code":200,
    "headers":[{"name":"Access-Control-Allow-Origin","value":"*"}],
    "body":"{
        \"id\":\"255572697884115_2\",
        \"from\":{
             \"name\":\"xyzk\",
             \"id\":\"59788447049\"},
        \"message\":\"This is the second message\",
        \"created_time\":\"2012-01-03T21:05:59+0000\"}"}
]

现在我已经尝试了几种方法来获得访问信息,但是每一个方法捕获结束...并抛出异常。

Now I have tried several methods to get access to message, but every method ends in catch... and throws an exception.

例如:

var serializer = new JavaScriptSerializer();   
var result = serializer.Deserialize<dynamic>(json);

foreach (var item in result)
{
    Console.WriteLine(item.body.message);
}

抛出异常:System.Collections.Generic.Dictionary犯规包含体的定义。不过你看到下面的截图,该机构包含定义。

throws the exception: System.Collections.Generic.Dictionary doesnt contain definitions for body. Nevertheless you see in the screenshot below, that body contains definitions.

Becaus我不允许张贴图片,你可以找到它的directupload: HTTP:/ /s7.directupload.net/images/120907/zh5xyy2k.png

Becaus I am not allowed to post pictures you can find it on directupload: https://m.xsw88.com/allimgs/daicuo/20230906/3093.png

我不还没有更多的想法,所以我请你帮助我。我需要这个项目,私人的,不是商业。

I don't havent more ideas so i please you to help me. I need this for a project, private, not commercial.

也许你可以给我的code的短语,这样我就可以继续我的发展。

Maybe you could give me an phrase of code, so i can continue my development.

感谢您到目前为止

多米尼克

推荐答案

如果您使用 Json.Net ,所有你所要做的就是

If you use Json.Net, All you have to do is

替换

var serializer = new JavaScriptSerializer();
var result = serializer.Deserialize<dynamic>(json);

dynamic result = JsonConvert.DeserializeObject(json);

这就是全部。