1

I'm relatively new to C# and am currently trying to serialize a string in JSON so it basically looks like this:

{ "firstname": "John", "lastname": "Smith", "address": { "street": "1 Main Street", "city": "London", "postcode": "A12 3AB" } }

I currently have the following code:

AddressValues addressStr = new AddressValues();
addressStr.street = "1 Main Street";
addressStr.city= "London";
addressStr.postcode = "A12 3AB";
string addressJson = JsonConvert.SerializeObject(addressStr);

PersonValues personDetails = new PersonValues();
personDetails.firstname = "John";
personDetails.lastname = "Smith";
personDetails.address = addressJson ;
string jsonContent = JsonConvert.SerializeObject(personDetails);

And the classes...

public class AddressValues
{
    public string street;
    public string city;
    public string postcode;
}

public class PersonValues
{
    public string firstname;
    public string lastname;
    public string address;
}

When I run the script the console displays the following:

{ "firstname": "John", "lastname": "Smith", "address": "{\"street\":\"1 Main Street\", \"city\":\"London\", \"postcode\": \"AB12 3AB\"}"}

Can anyone point out how I can go around amending this please? I'm assuming having a backslash means that it ignores the quotation mark?

Thanks

2
  • 3
    Erm why are you storing the JsonValue on PersonValues class? Why not have a nested object and then serialize PersonValues? Commented Aug 5, 2014 at 13:34
  • 1
    The backslash is expected because you're trying to save a string inside a string. The backslashes means the quotes are escaped and thus not interpreted as being a delimiter of the outer string. Commented Aug 5, 2014 at 13:40

1 Answer 1

6

Declare classes as follows

public class Address
{
    public string street;
    public string city;
    public string postcode;
}

public class PersonValues
{
    public string firstname;
    public string lastname;
    public Address address; //SEE THIS LINE
}

and use as

PersonValues personDetails = new PersonValues();
personDetails.firstname = "John";
personDetails.lastname = "Smith";
personDetails.address = new Address();
personDetails.address.street = "1 Main Street";
personDetails.address.city = "London";
personDetails.address.postcode = "A12 3AB";


string jsonContent = JsonConvert.SerializeObject(personDetails);

Final json is

{"firstname":"John","lastname":"Smith","address":{"street":"1 Main Street","city":"London","postcode":"A12 3AB"}}
Sign up to request clarification or add additional context in comments.

3 Comments

This is what I was thinking to, not sure why someone has downvoted?
This worked like a dream and is a lot more concise and cleaner! Thanks L.B
and when using object initializer, code looks almost same as json ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.