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.

7
  • Do you have access to the complete response string or stream? If so, could you use javascriptserializer which is built-in and not 3d party? See e.g. this answer to Deserialize JSON into C# dynamic object?. Commented Sep 7, 2018 at 19:01
  • Seems I can't use javascriptserializer for this problem, I get the same errors as the person in this link: inogic.com/blog/2016/10/… Commented Sep 7, 2018 at 20:21
  • That's too bad. Can you get the raw response string or stream? Commented Sep 7, 2018 at 21:19
  • Also, what do you need to do with the public string CustomObject { get; set; } once you get it? Commented Sep 7, 2018 at 21:43
  • I can get the raw response string when I do the API call. Commented Sep 7, 2018 at 21:44

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.