简体   繁体   English

JSON反序列化C#错误

[英]JSON deserialize c# error

I'm following this JSON deserialization example: Deserialize JSON with C# 我正在关注以下JSON反序列化示例: 使用C#反序列化JSON

but I've run into a problem. 但我遇到了一个问题。 My json file has a different structure. 我的json文件具有不同的结构。

"data": [
  {
     "id": "157156474316544_575441765821344",
     "from": {
        "name": "Testy McTest",
        "id": "624161969"
     },
     "message": "I am a message"...

So when I instantiate the serializer: 所以当我实例化序列化器时:

FacebookFeed facebookPosts = new JavaScriptSerializer().Deserialize<FacebookFeed>(jsonFromWeb);

I get the error "No parameterless constructor defined for type of 'System.String'." 我收到错误“没有为'System.String'类型定义无参数构造函数。”

If anybody know how to fix this I would greatly appreciate it. 如果有人知道如何解决此问题,我将不胜感激。

My structure is : 我的结构是:

public class FacebookFeed
{
public List<FacebookPost> data { get; set; }
}

public class FacebookPost 
{
public string id        { get; set; }
public string from      { get; set; }
public string message   { get; set; }
}

The problem is that from is an complex(it has several user attributes/properties - id, name) object but you've declared it as plain string. 问题是from是一个复杂的对象(它具有多个用户属性/属性-id,name),但是您已将其声明为纯字符串。 Try something like: 尝试类似:

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

public class FacebookPost 
{
public string id        { get; set; }
public Sender from      { get; set; }
public string message   { get; set; }
}

'from' is an object, not a string. “ from”是对象,而不是字符串。 Thus you are getting the error "No parameterless constructor defined for type of 'System.String'." 因此,您将收到错误“没有为'System.String'类型定义无参数构造函数”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM