在FB C#客户端使用Newtonsoft JsonConvert当JSON结构返回空,无属性名称和值 [英] Json structure is returned empty, without property names and values when using Newtonsoft JsonConvert in FB C# client

查看:151
本文介绍了在FB C#客户端使用Newtonsoft JsonConvert当JSON结构返回空,无属性名称和值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在asp.net mvc的工作4应用程序,它使用的Facebook C#SDK(6.0.10.0)和Newtonsoft.Json(4.5.0.0)。

I'm working on asp.net mvc 4 application which uses Facebook C# SDK (6.0.10.0) and Newtonsoft.Json (4.5.0.0).

与FacebookClient请求返回的expando对象:

Request with FacebookClient returns expando object:

[Authorize]
public ActionResult GetFbData(string path = "me"){
    var expando = this.fb.Get(path);

    return Json(expando);
}

返回的JSON如下:

Returned Json looks like:

[{"Key":"id","Value":"100000xxxxxxxx"},{"Key":"name","Value":"John Doe"} ... ]

我要在格式返回它 {ID:100000xxxxxxx,名称:李四,...} ,所以我说这到code这创建我的FB客户端:

I want to return it in format {id:100000xxxxxxx, name:"John Doe", ... } so I added this to the code which creates my fb client:

fb.SetJsonSerializers(JsonConvert.SerializeObject,
                      JsonConvert.DeserializeObject);

从上面现在同一code返回:

Same code from above now returns:

[[[]],[[]],[[]],[[]],[[]],[[]],[[[[]],[[]]]],[[[[]],[[]]]],[[]],[[]],[[[[[[[]],[[]]...]

我可以得到想要的结果,用:

I can get desired result with:

return Content(JsonConvert.SerializeObject(Expando));

这将返回正确的JSON,但Content-Type的是的text / html;字符集= UTF-8 ,我想知道我怎么能为JsonResult返回所需的格式,无需手动设置响应头等等,我只是想改变默认的序列化行为而无需重新实现串行器等。

This returns proper Json, but Content-Type is text/html; charset=utf-8, and I'm wondering how I can return the desired format as JsonResult without manually setting response headers etc, I just want to change default serialization behavior without re-implementing serializer etc.

必须有一些简单的与code的单行以改变这种行为,而且我希望有人已经找到了它。

There must be something simple that is done with single line of code to change this behavior, and I'm hoping that someone already found it.

推荐答案

不知道如果你还在寻找一个答案,但你几乎拥有了。内容需要,它是内容类型的第二个参数,所以如果你改变它是

Not sure if you're still looking for an answer, but you almost had it. Content takes a second parameter that is the content type, so if you change it to be

return Content(JsonConvert.SerializeObject(Expando), "application/json");

它应该按预期工作

it should work as expected

这篇关于在FB C#客户端使用Newtonsoft JsonConvert当JSON结构返回空,无属性名称和值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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