的JSONObject到模型的Facebook SDK

编程入门 行业动态 更新时间:2024-10-10 05:23:42
本文介绍了的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 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

本文发布于:2023-11-28 14:26:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1642785.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模型   JSONObject   SDK   Facebook

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!