0

I have to deserialize a JSON response(from a wep API) . The problem is that this API returns JSON with dynamic property. Had it been like

{
"employees":
[{
"employeeCode": "ABC",
"cityId": 123
},{
"employeeCode": "DEF",
"cityId": 234
}]
}

it would have been perfect but the response is string and is returned like:

var response = @"{"ABC": 123, "DEF": 234}";

Where the first property is "EmployeeCode" and the second property is "CityId". How can I use JSON.Net to serialize it into the following class?

public class Employees
{
public string employeeCode {get; set;}
public string cityId {get; set;}
}
User2682
  • 193
  • 2
  • 4
  • 13
  • 1
    Possible duplicate of [.Net NewtonSoft Json Deserialize map to a different property name](http://stackoverflow.com/questions/15915503/net-newtonsoft-json-deserialize-map-to-a-different-property-name) – Luke Apr 13 '17 at 12:14
  • I also wouldn't call that a "dynamic property" since since it is not like http://stackoverflow.com/questions/2690623/what-is-the-dynamic-type-in-c-sharp-4-0-used-for but instead the property name is not passed... – Austin T French Apr 13 '17 at 12:18
  • 1
    @stuartd I actually have to get all the employeeCodes as well as their cityId. I am not sure about how to deserialize the above string into the same class(RootObject) – User2682 Apr 13 '17 at 12:19
  • 1
    I think this answer is much closer [How can I deserialize JSON to a simple Dictionary in ASP.NET?](http://stackoverflow.com/a/1212115/6694376) After doing that you can just use LINQ to map it to your object. – Zoran Basic Apr 13 '17 at 12:23
  • @ZoranBasic this is exactly what I was looking for. – User2682 Apr 13 '17 at 12:29
  • 2
    Possible duplicate of [How can I deserialize JSON to a simple Dictionary in ASP.NET?](http://stackoverflow.com/questions/1207731/how-can-i-deserialize-json-to-a-simple-dictionarystring-string-in-asp-net) – Lennart Apr 13 '17 at 12:47

2 Answers2

1

Regarding my comment maybe it us better to write the example of what I ment:

string json = @"{""ABC"": 123, ""DEF"": 234}";

var employees = JsonConvert.DeserializeObject<Dictionary<string, int>>(json).Select(x => new Employees() { employeeCode = x.Key, cityId  = x.Value });
Zoran Basic
  • 156
  • 1
  • 11
  • this use only for json object, not used in json array like { "name":"John", "age":30, "cars":[ "Ford", "BMW", "Fiat" ] } – Nayan Patel Apr 13 '17 at 12:59
0

You'll need:

using System.IO;

using System.Text;

using System.Runtime.Serialization.Json;

public static string Serialize<T>(T obj)
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
MemoryStream ms = new MemoryStream();
serializer.WriteObject(ms, obj);
string retVal = Encoding.UTF8.GetString(ms.ToArray());
return retVal;
}

public static T Deserialize<T>(string json)
{
T obj = Activator.CreateInstance<T>();
MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json));
DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
obj = (T)serializer.ReadObject(ms);
ms.Close();
return obj;
}
Nayan Patel
  • 154
  • 7