0

I am currently trying to parse in user data in this form:

{
  "id": "0",
  "name": "Tristan Paul",
  "location": {
    "country": "USA",
    "city": "District of Columbia"
  },
  "job": {
    "industry": "Tech",
    "sector": "Tech"
  },
  "experience": 2,
  "frequency": 2,
  "interests": [
    "Social Media",
    "Millenials",
    "Change Management"
  ],
  "role": "Mentor",
  "age": 34
}

I want to Parse it into a Dictionary<string, object> which I have done with JObject.Parse(json) and object.ToObject<Dictionary<string, object>>(). When testing the types of each of the values in this Dictionary it produces the following print out.

System.String 
System.String 
Newtonsoft.Json.Linq.JObject 
Newtonsoft.Json.Linq.JObject 
System.Int64
System.Int64
Newtonsoft.Json.Linq.JArray
System.String
System.Int64

What I want to know is, is there a way to specify how the Newtonsoft.Json.Linq.JObject and Newtonsoft.Json.Linq.JArray are converted as well? Or do I need to iterate through each value and do a .ToObject<T>() on them?

Thanks!

Edit: I am currently am trying to Deserialize Json to a Person object. The Properties of this Person object look like this.

[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("role")]
public string Role { get; set; }

[JsonExtensionData]
public Dictionary<string, object> Properties { get; set; }

I am looking for a way to use a JsonConverter or ContractResolver to specific how each of these extra properties will be added. Same as the problem above for source data and trying to catch those JObjects and JArrays and convert them to a different type.

I cannot see a way to do this with the JsonConverter, am I missing something?

I want to convert the JArray to a String[] and the JObjects to a Dictionary.

System.String 
System.String 
Newtonsoft.Json.Linq.JObject => System.Dictionary<string, string>
Newtonsoft.Json.Linq.JObject => System.Dictionary<string, string>
System.Int64
System.Int64
Newtonsoft.Json.Linq.JArray => System.String[]
System.String
System.Int64
Tristan Trainer
  • 2,770
  • 2
  • 17
  • 33
  • 2
    check `JsonConverter` https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_JsonConverter.htm maybe it can help you – demo Feb 28 '19 at 12:05
  • Possible duplicate of [How do I use JSON.NET to deserialize into nested/recursive Dictionary and List?](https://stackoverflow.com/a/19140420/10263) – Brian Rogers Feb 28 '19 at 16:44
  • I don't think it's a duplicate, followed the code for that answer and it didn't solve my issue unfortunately. – Tristan Trainer Mar 01 '19 at 12:17

0 Answers0