Serialize a JSON Object to Dictionary [duplicate]

I have an object of the following class structure:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public class Customer
{
public string ID { get; set; }
public Address contact { get; set; }
}
public class Address
{
public string city { get; set; }
}
</code>
<code>public class Customer { public string ID { get; set; } public Address contact { get; set; } } public class Address { public string city { get; set; } } </code>
public class Customer
{
    public string ID { get; set; }
    public Address contact { get; set; }
}

public class Address
{
    public string city { get; set; }
}

and I want the following expected output:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Dictionary<string, string> result = new Dictionary<string, string>()
{
{ "ID", "1" },
{ "contact.city", "Miami" }
};
</code>
<code>Dictionary<string, string> result = new Dictionary<string, string>() { { "ID", "1" }, { "contact.city", "Miami" } }; </code>
Dictionary<string, string> result = new Dictionary<string, string>()
{
    { "ID",  "1" },
    { "contact.city", "Miami" }
};

but I fail because of a nested class structure.

Exception:

Unhandled exception. Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: {. Path ‘contact’, line 1, position 21.
at Newtonsoft.Json.JsonTextReader.ReadStringValue(ReadType readType)

My code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Customer customer = new Customer() { ID = "1", contact = new Address() { city = "Miami" } };
string customerString = Newtonsoft.Json.JsonConvert.SerializeObject(customer);
Dictionary<string, string> result = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>(customerString);
</code>
<code>Customer customer = new Customer() { ID = "1", contact = new Address() { city = "Miami" } }; string customerString = Newtonsoft.Json.JsonConvert.SerializeObject(customer); Dictionary<string, string> result = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>(customerString); </code>
Customer customer = new Customer() { ID = "1",  contact = new Address() { city = "Miami" } };
string customerString = Newtonsoft.Json.JsonConvert.SerializeObject(customer);
Dictionary<string, string> result = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>(customerString);

Question:
How to achieve this output?

https://dotnetfiddle.net/Q34OxD

1

AFAIK, Newtonsoft.Json does not support (de)serialization for flattened JSON.

You have to implement additional logic to flatten JSON.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Linq;
public static class JsonHelper
{
public static Dictionary<string, string> FlattenObject<T>(T source)
where T : class, new()
{
return JObject.FromObject(source)
.Descendants()
.OfType<JValue>()
.ToDictionary(k => k.Path, v => v.Value<string>());
}
}
</code>
<code>using System; using System.Collections.Generic; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System.Linq; public static class JsonHelper { public static Dictionary<string, string> FlattenObject<T>(T source) where T : class, new() { return JObject.FromObject(source) .Descendants() .OfType<JValue>() .ToDictionary(k => k.Path, v => v.Value<string>()); } } </code>
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Linq;

public static class JsonHelper
{
    public static Dictionary<string, string> FlattenObject<T>(T source)
        where T : class, new()
    {
        return JObject.FromObject(source)
            .Descendants()
            .OfType<JValue>()
            .ToDictionary(k => k.Path, v => v.Value<string>());
    }
}

Caller:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Dictionary<string, string> result = JsonHelper.FlattenObject(JObject.Parse(customerString));
</code>
<code>Dictionary<string, string> result = JsonHelper.FlattenObject(JObject.Parse(customerString)); </code>
Dictionary<string, string> result = JsonHelper.FlattenObject(JObject.Parse(customerString));

You can provide the customer instance directly without serialized JSON:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Dictionary<string, string> result = JsonHelper.FlattenObject(customer);
</code>
<code>Dictionary<string, string> result = JsonHelper.FlattenObject(customer); </code>
Dictionary<string, string> result = JsonHelper.FlattenObject(customer);

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật