0

Currently, I am receiving a dynamic JSON response from an API which I am trying to parse. An example of the JSON that is received looks as follows:

 {
  "data": [
    {
      "Id": "XXXXXXXXXXXXXXXXXXXXXXXX",
      "IsActive": true,
      "DateCreated": "2017-11-09T00:01:49.827Z",
      "DateModified": "2017-11-09T00:01:49.827Z",
      "IsDeleted": false,
      "Uid": "XXXXXXXXXXXXXXXXXXXXXXXX",
      "CustomObject": {
        "customdata1": " Store my customdata1 value",
        "customdata2": " Store my customdata2 value"
      }
    }
  ],
  "Count": 1
}

In the above JSON, while serializing, I want CustomObject to be mapped to a string in the C# object. Below is an example of the object structure I want.

[System.Runtime.Serialization.DataContract]
public class CustomObjectData
{
    [System.Runtime.Serialization.DataMember]
    public string Id { get; set; }
    [System.Runtime.Serialization.DataMember]
    public bool IsActive { get; set; }
    [System.Runtime.Serialization.DataMember]
    public string DateCreated { get; set; }
    [System.Runtime.Serialization.DataMember]
    public string DateModified { get; set; }
    [System.Runtime.Serialization.DataMember]
    public bool IsDeleted { get; set; }
    [System.Runtime.Serialization.DataMember]
    public string Uid { get; set; }
    [System.Runtime.Serialization.DataMember]
    public string CustomObject { get; set; }
}

[System.Runtime.Serialization.DataContract]
public class CustomObjectDataHolder
{
    [System.Runtime.Serialization.DataMember]
    public List<CustomObjectData> data { get; set; }
    [System.Runtime.Serialization.DataMember]
    public int Count { get; set; }
}

I am trying to develop this as a plugin for CRM, so I am unable to use other third party JSON serializers like NewtonSoft.

dbc
  • 104,963
  • 20
  • 228
  • 340
Riaru Neimu
  • 43
  • 1
  • 9
  • Do you have access to the complete response string or stream? If so, could you use [tag:javascriptserializer] which is built-in and not 3d party? See e.g. [this answer](https://stackoverflow.com/a/13642873) to [Deserialize JSON into C# dynamic object?](https://stackoverflow.com/q/3142495). – dbc Sep 07 '18 at 19:01
  • Seems I can't use javascriptserializer for this problem, I get the same errors as the person in this link: https://www.inogic.com/blog/2016/10/working-with-json-objects-in-dynamics-crm-plugins/ – Riaru Neimu Sep 07 '18 at 20:21
  • That's too bad. Can you get the raw response string or stream? – dbc Sep 07 '18 at 21:19
  • Also, what do you need to do with the `public string CustomObject { get; set; }` once you get it? – dbc Sep 07 '18 at 21:43
  • I can get the raw response string when I do the API call. – Riaru Neimu Sep 07 '18 at 21:44
  • I will be using the JSON string to fill in a field of a Dynamics CRM entry. – Riaru Neimu Sep 07 '18 at 21:46
  • It looks like your `CustomObject` could be represented as a `Dictionary`. If so, **and you are using .NET 4.5 or later**, you can deserialize your `"CustomObject"` with `DataContractJsonSerializerSettings.UseSimpleDictionaryFormat` as shown in [Any way to make DataContractJsonSerializer serialize Dictionaries properly?](https://stackoverflow.com/q/4559991) and [Serialize a Dictionary in specific format](https://stackoverflow.com/q/20562583). – dbc Sep 08 '18 at 04:51

0 Answers0