Click here to Skip to main content
15,880,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
This should be a simple issue.
I have a data class, and json data, but I cant find a way to change the strong into a json object.
All help would be appreciated.
Here is the string
[{"CampaignID":"F1","Customer":"cust1","Language":"E","Content":[
	{
"Type":"Email",
"Campaign":"t1",
"Card_type":"xxxxx",
"Last4":"xxxx",
"Name":"Rick",
"EmailAddress":"xxxxx@hotmail.com"
}
]},{"CampaignID":"F1","Customer":"cust1","Language":"E","Content":[
	{
"Type":"Email",
"Campaign":"t1",
"Card_type":"xxxxxxxx",
"Last4":"xxxx",
"Name":"Donna",
"EmailAddress":"xxxxx@hotmail.com"
}
]},{"CampaignID":"t1","Customer":"f1","Language":"E","Content":[
	{
"Type":"Email",
"Campaign":"t1",
"Card_type":"xxxxxx",
"Last4":"xxxx",
"Name":"Chelsea Bun",
"EmailAddress":"xxxxxxxxx-poo@hotmail.com"
}
]}]


And here are the classes

public class clsEmail
{

    public string CompanyName { get; set; } = "";
    public string Campaign { get; set; } = "";
    public string CampaignID { get; set; } = "";
    public string Customer { get; set; } = "";

    public string Language { get; set; } = "";
    public IList<Content> Content { get; set; }

}
public class Content
{
    public string Type { get; set; }
    public string Campaign { get; set; }
    public string Card_type { get; set; }
    public string Last4 { get; set; }
    public string Name { get; set; }
    public string EmailAddress { get; set; }
}


What I have tried:

I have tried to convert to json object, loop through an list as well as an array
Posted
Updated 22-May-20 19:11pm
Comments
MadMyche 22-May-20 18:03pm    
You should just be able to DeSerialize this... but I cannot tell where you are getting the clsEmail and CompanyName names

1 solution

Try this code generated by JSON Utils: Generate C#, VB.Net, SQL Table, Java and PHP from JSON[^]

public class Content
 {
     public string Type { get; set; }
     public string Campaign { get; set; }
     public string Card_type { get; set; }
     public string Last4 { get; set; }
     public string Name { get; set; }
     public string EmailAddress { get; set; }
 }

 public class Example
 {
     public string CampaignID { get; set; }
     public string Customer { get; set; }
     public string Language { get; set; }
     public IList<Content> Content { get; set; }
 }


Then you should be able to deserialize like this:
var obj = File.ReadAllText("Testdata.json");
List<Example> jsonResults = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Example>>(obj);

foreach (var item in jsonResults)
{
    Debug.Print(item.Customer);

    foreach (var content in item.Content)
    {
        Debug.Print("content.Name = " + content.Name);
    }
}
 
Share this answer
 
v4
Comments
Member 8087552 24-May-20 16:32pm    
With 1 minor modification, this solved my issue. THANK YOU. It was driving me crazy.
Since all already had a the text as a string, I did not need your var obj line.
Thank you again for your help.
RickZeeland 25-May-20 1:16am    
Glad it worked, you can mark it as the "accepted solution".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900