我将尽力解释这一点。我有一个称为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。我已经通过创建Customer类来进行尝试:
public class Customer
{
public string email { get; set; }
public List<Property> properties { get; set; }
}
和一类Property:
public class Property
{
public string property { get; set; }
public string value { get; set; }
}
我终生无法得到我追求的结果。我认为它是“客户”类中的“财产清单”。如果我将列表更改为字符串并在此处仅定义一个值,则输出很好。
请帮助:(
答案 0 :(得分:0)
此代码有效:
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.我必须在属性的元素中添加缺少的{
括号。
答案 1 :(得分:0)
[已解决]
感谢所有提供评论的人。您的指导帮助我解决了问题。
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;
}