【问题标题】:JSON Class with a List of another Class带有另一个类列表的 JSON 类
【发布时间】:2019-03-13 20:28:25
【问题描述】:

我会尽力解释这一点。我有一个名为 Prospect 的类,其中包含电子邮件、公司、名字、姓氏、电话等字符串。

我需要以

的格式以 JSON 格式输出潜在客户信息
[
   {"email":"test@test.com",
      "properties":[
      {
        "property":"company",
        "value": "Company Name"
      },
        "property":"firstname",
        "value":"John"
      },
        "property":"surname",
         "value":"Smith"
      },
        "property":"phone",
        "value":"01234567891"
      }]
  }
]

我需要输出我捕获的所有潜在客户的 JSON。我已经通过创建一个客户类来尝试这个:

public class Customer
{
    public string email { get; set; }
    public List<Property> properties { get; set; }

}

还有一类属性:

public class Property
{
    public string property { get; set; }
    public string value { get; set; }
}

我这辈子都得不到我想要的结果。我认为它是 Customer 类中的属性列表。如果我将 List 更改为字符串并在此处仅定义一个值,则输出很好。

请帮忙:(

【问题讨论】:

  • 包含用于反序列化 json 的代码。此外, json 似乎没有很好的格式。第二、第三和第四个属性没有左大括号。
  • 感谢您抽出宝贵时间提供帮助。我正在尝试与 HubSpot CRM 进行通信。他们的 API 文档显示了 JSON 应该如何格式化,但我不知道该怎么做。页面在这里link
  • JSONLint 报告您发布的内容无效。该链接对我被阻止,所以我无法检查任何内容

标签: c# json nsjsonserialization


【解决方案1】:

此代码有效:

public class Property
    {
      public string property { get; set; }
      public string value { get; set; }
    }

    public class Customer
    {
      public string email { get; set; }
      public List<Property> properties { get; set; }

    }

    static void Main(string[] args)
    {
      string JSON = @"[
   {""email"":""test @test.com"",
      ""properties"":[
      {
        ""property"":""company"",
        ""value"": ""Company Name""
      },
       { ""property"":""firstname"",
        ""value"":""John""
      },
       { ""property"":""surname"",
         ""value"":""Smith""
      },
       { ""property"":""phone"",
        ""value"":""01234567891""
      }]
  }
]
";
      Customer[] obj = JsonConvert.DeserializeObject<Customer[]>(JSON);

    }

请注意: 1. 我必须在属性的元素中添加缺少的开头{ 括号。

【讨论】:

  • 感谢您抽出宝贵时间帮助@Max,我根本无法使用它。我如何让它输出到控制台或字符串?我认为我缺乏知识和经验是我无能的最大原因。再次感谢
  • @Swanne,没有输出。要测试它是如何工作的,只需在反序列化后设置一个断点(在最后一个}),然后使用 Shift+F9 查看obj 属性(中断时,将光标悬停在obj 变量上以查看其浮动窗口中的属性,或将输入光标放在其上并按 Shift+F9)。
【解决方案2】:

[已解决]

感谢所有提供 cmets 的人。您的指导帮助我解决了我的问题。

        public class Customer
    {
        public string email { get; set; }
        public List<Property> properties { get; set; }
    }

    public class Property
    {
        public string property { get; set; }
        public string value { get; set; }
    }



    private void button1_Click(object sender, EventArgs e)
    {


        Customer _c = new Customer();
        _c.email = email.Text;
        _c.properties = new List<Property>();
        _c.properties.Add(new Property{ property = "company", value = company.Text });
        _c.properties.Add(new Property { property = "website", value = website.Text });
        _c.properties.Add(new Property { property = "firstname", value = firstname.Text });
        _c.properties.Add(new Property { property = "lastname", value = lastname.Text });
        _c.properties.Add(new Property { property = "phone", value = phone.Text });

        string json = JsonConvert.SerializeObject(_c, Formatting.Indented);
        outputBox.Text = json;

    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-01
    • 2015-03-02
    • 1970-01-01
    • 2012-02-17
    • 2017-06-24
    • 1970-01-01
    • 2022-06-14
    • 1970-01-01
    相关资源
    最近更新 更多