Vaibhav Gole
Vaibhav Gole

Reputation: 165

Parse complex JSON string to read values in C#

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.

Upvotes: 1

Views: 728

Answers (1)

Emre Kabaoglu
Emre Kabaoglu

Reputation: 13146

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);

Upvotes: 2

Related Questions