的JSONObject到模型的Facebook SDK [英] JsonObject to Model Facebook SDK

查看:136
本文介绍了的JSONObject到模型的Facebook SDK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不得不使用Facebook C#SDK为在.NET 3.5中新的poject,我知道,最新的版本有例子为4 - 但它也编译对3.5这样的工作完全

I'm have to use the facebook c# sdk for a new poject in .net 3.5 , I'm aware that the latest version has examples for 4 - but it's also compiled against the 3.5 so works completely.

无论如何,请原谅我,如果我是令人难以置信的愚蠢。但我正在寻找一个JSON对象转换成我的模型,我可以做这样的事情?

Anyway, and forgive me if I'm being incredibly dumb. But i'm looking to convert a json object into my model, can I do something like this?

public ActionResult About()
{
    var app = new FacebookApp();
    JsonObject friends = (JsonObject)app.Get("me/friends");
    ViewData["Albums"] = new Friends((string)friends.ToString());
    return View();
}

public class Friends
{
    public string name { get; set; }
    public string id { get; set; }

    public Friends(string json)
    {
        JArray jObject = JArray.Parse(json);
        JToken jData = jObject["data"];

        name = (string)jData["name"];
        id = (string)jData["id"];
    }
}

这是使用Json.Net。显然,这是不行的,我回来了误差

This is using Json.Net. Obviously this doesn't work, the error I get back is

错误从JsonReader阅读JArray。目前JsonReader项目不是一个数组:在StartObject

我是pretty的肯定,我要彻底解决这个错误的方式 - 所以,如果任何人都可以提供任何提示,我会incredibbly感谢

I'm pretty sure that I'm going completely the wrong way around this - so if anyone can offer any tips I'd be incredibbly grateful.

推荐答案

这也许code可以帮助:

Maybe this code would help:

public class Friend
{
    public string Id { get; set; }
    public string Name { get; set; }
}

...

public ActionResult About()
{
    var app = new FacebookApp();
    var result = (JsonObject)app.Get("me/friends"));
    var model = new List<Friend>();

    foreach( var friend in (JsonArray)result["data"])
        model.Add( new Friend()
        {
            Id = (string)(((JsonObject)friend)["id"]),
            Name = (string)(((JsonObject)friend)["name"])
        };

    return View(model);
}

现在你的模型将是类型名单,其中;朋友&GT;

Now your model will be of type List<Friend>

这篇关于的JSONObject到模型的Facebook SDK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆