JSON Class 与另一个列表 Class
JSON Class with a List of another Class
我会尽力解释这一点。我有一个名为 Prospect 的 class,其中包含电子邮件、公司、名字、姓氏的字符串,phone。
我需要在JSON中以
的格式输出Prospect信息
[
{"email":"test@test.com",
"properties":[
{
"property":"company",
"value": "Company Name"
},
"property":"firstname",
"value":"John"
},
"property":"surname",
"value":"Smith"
},
"property":"phone",
"value":"01234567891"
}]
}
]
我需要输出我捕获的所有潜在客户的 JSON。我已经通过创建一个 class 的 Customer:
来尝试这个
public class Customer
{
public string email { get; set; }
public List<Property> properties { get; set; }
}
和 class 的 属性:
public class Property
{
public string property { get; set; }
public string value { get; set; }
}
我这辈子都无法得到我想要的结果。我认为它是客户 class 中 属性 的列表。如果我将列表更改为字符串并在此处仅定义一个值,则输出正常。
请帮忙:(
此代码有效:
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. 我必须在属性元素中添加缺失的左 {
括号。
[已解决]
感谢所有提供意见的人。您的指导帮助我解决了问题。
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;
}
我会尽力解释这一点。我有一个名为 Prospect 的 class,其中包含电子邮件、公司、名字、姓氏的字符串,phone。
我需要在JSON中以
的格式输出Prospect信息[
{"email":"test@test.com",
"properties":[
{
"property":"company",
"value": "Company Name"
},
"property":"firstname",
"value":"John"
},
"property":"surname",
"value":"Smith"
},
"property":"phone",
"value":"01234567891"
}]
}
]
我需要输出我捕获的所有潜在客户的 JSON。我已经通过创建一个 class 的 Customer:
来尝试这个public class Customer
{
public string email { get; set; }
public List<Property> properties { get; set; }
}
和 class 的 属性:
public class Property
{
public string property { get; set; }
public string value { get; set; }
}
我这辈子都无法得到我想要的结果。我认为它是客户 class 中 属性 的列表。如果我将列表更改为字符串并在此处仅定义一个值,则输出正常。
请帮忙:(
此代码有效:
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. 我必须在属性元素中添加缺失的左 {
括号。
[已解决]
感谢所有提供意见的人。您的指导帮助我解决了问题。
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;
}