定义嵌套的JSON格式

时间:2019-02-27 15:04:27

标签: c# json xamarin serialization

我希望通过他们的创建联系api将潜在客户捕获应用程序连接到HubSpot。我有一个Prospect对象列表,这些对象已序列化为JSON,结果如下:

[{
    "CompanyName": "Test",
    "Website": "www.test.com",
    "FirstName": "Carl",
    "LastName": "Swann",
    "JobTitle": "Dr",
    "Phone": "0789654321",
    "Email": "twst@email.com",
    "Products": "Khaos Control Hybrid",
    "Notes": "Here are the notes ",
    "ContactOwner": "cswann@khaoscontrol.com",
    "ShowName": "Spring Fair"
}]

如何确保我拥有的信息适合此处所需的HubSpot JSON结构:

{
"email": "testingapi1s@hubspot.com",
"properties": [
  {
    "property": "firstname",
    "value": "Harper"
  },
  {
    "property": "lastname",
    "value": "Wolfberg"
  },
  {
    "property": "website",
    "value": "http://hubspot.com"
  },
  {
    "property": "company",
    "value": "HubSpot"
  },
  {
    "property": "phone",
    "value": "555-122-2323"
  },
  {
    "property": "address",
    "value": "25 First Street"
  },
  {
    "property": "city",
    "value": "Cambridge"
  },
  {
    "property": "state",
    "value": "MA"
  },
  {
    "property": "zip",
    "value": "02139"
  }
]

1 个答案:

答案 0 :(得分:1)

请按照以下步骤操作 1.序列化源json列出 2。

public class Destination
    {
        public string email { get; set; }   
        public List<PropertyDescription> properties { get; set; }
    }
public class PropertyDescription
    {
            public string property { get; set; }
            public object value { get; set; }   
    }
List<Source> sources = serialize “sourceJson”;
       var destination=new List<Destination>();
       foreach (var source in sources)
       {
           var dest = new Destination();
           foreach (var property in source.GetType().GetProperties())
           {
               var propertValue = property.GetValue(source);
               dest.properties.Add(new PropertyDescription
               {
                   property = property.Name,
                   value = propertValue
               });
           }
           destination.Add(dest);
        }

3。 反序列化目的地将接近您的预期结果