1

I am trying to read below JSON string in C#

    {
  "ModelState": {
    "obj.SystematicDate": {
      "_errors": [
        {
          "<Exception>k__BackingField": null,
          "<ErrorMessage>k__BackingField": "A value is required."
        }
      ],
      "<Value>k__BackingField": null
    },
    "obj.CustomerId": {
      "_errors": [
        {
          "<Exception>k__BackingField": null,
          "<ErrorMessage>k__BackingField": "A value is required."
        }
      ],
      "<Value>k__BackingField": null
    },
    "obj.userId": {
      "_errors": [
        {
          "<Exception>k__BackingField": null,
          "<ErrorMessage>k__BackingField": "User Id is mandatory"
        }
      ],
      "<Value>k__BackingField": null
    }
  }
}

The above mentioned JSON string is response from an api call and it is dynamic. It can contain n number of 'obj.' properties.

I need to read that values and show error message as SystematicDate: A value is required , userId: User Id is mandatory and so on.

I tried below solutions but did not get the desired output.

var jsonStrin = "{'ModelState':{'obj.SystematicDate':{'_errors':[{' < Exception > k__BackingField':null,' < ErrorMessage > k__BackingField':'A value is required.'}],' < Value > k__BackingField':null},'obj.CustomerId':{'_errors':[{' < Exception > k__BackingField':null,' < ErrorMessage > k__BackingField':'A value is required.'}],' < Value > k__BackingField':null},'obj.userId':{'_errors':[{' < Exception > k__BackingField':null,' < ErrorMessage > k__BackingField':'User Id is mandatory'}],' < Value > k__BackingField':null}}}";
//var stuff=JsonConvert.DeserializeObject(jsonStrin);
//JObject o = JObject.Parse(jsonStrin);
var example1Model = new JavaScriptSerializer().Deserialize<ModelState>(jsonStrin);


public class ModelState {
    public List<SystematicDateError> SystematicDate { get; set; }
    public List<CustomerIdError> CustomerId { get; set; }
}

public class SystematicDateError
{
    public List<string> _errors { get; set; }
}

public class CustomerIdError
{
    public List<string> _errors { get; set; }
}

Please help me with this. Thanks in advance.

3
  • You're trying to deserialize to ModelState, but ModelState isn't the root object in your JSON. You need a containing object. Commented Dec 21, 2017 at 5:17
  • @john Thanks for your prompt response. I do not have control over json response i can not modify it. I need to read it the way it is. Commented Dec 21, 2017 at 5:33
  • You can modify your class though. Right? Commented Dec 21, 2017 at 5:35

1 Answer 1

2

Your model classes are wrong. I modified them like this;

public class Root
{
    public ModelState ModelState { get; set; }
}

public class ModelState
{
    [JsonProperty("obj.SystematicDate")]//You should specify the obj. properties here
    public Obj SystematicDate { get; set; }
    [JsonProperty("obj.CustomerId")]
    public Obj CustomerId { get; set; }
    [JsonProperty("obj.userId")]
    public Obj UserId { get; set; }
}

public class ObjError
{
    public string k__BackingField { get; set; }

    public string k__BackingField2 { get; set; }
}
public class Obj
{
    public List<ObjError> _errors { get; set; }

    public string state { get; set; }
}

Then just deserialize it like this;

var jsonStrin = "{'ModelState':{'obj.SystematicDate':{'_errors':[{' < Exception > k__BackingField':null,' < ErrorMessage > k__BackingField':'A value is required.'}],' < Value > k__BackingField':null},'obj.CustomerId':{'_errors':[{' < Exception > k__BackingField':null,' < ErrorMessage > k__BackingField':'A value is required.'}],' < Value > k__BackingField':null},'obj.userId':{'_errors':[{' < Exception > k__BackingField':null,' < ErrorMessage > k__BackingField':'User Id is mandatory'}],' < Value > k__BackingField':null}}}";
var stuff=JsonConvert.DeserializeObject<Root>(jsonStrin);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much it worked as expected. Only i need to change the ObjError class just added [JsonProperty(" < Exception > k__BackingField")] and [JsonProperty(" < ErrorMessage > k__BackingField")] and it worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.