如何使用jarray解析json?

时间:2015-05-20 07:46:33

标签: c# asp.net

[
  {
    "religion": "Hindu",
    "otherreligionidtype": "1",
    "caste": "SC",
    "personwithdhhhy": "No",
    "purpogfgggan": "Studies",
    "typeongghance": "dontknow",
    "proposehhhgenure": "dontknow",
    "repajjgjequency": "Monthly",
    "sajgjgank": "yes",
    "savijgjcno": "975568592379236",
    "mjjggde": "7397576",
    "ifggde": "658545",
    "spouseinfo": [
      {
        "spousename": "Roshni",
        "spousekycidtype": "PAN Card",
        "spousekycidno": "56753",
        "spousedob": "26/05/1989"
      }
    ],
    "nominejeinfo": [
      {
        "nomijjjjgjme": "Krrish",
        "nomineekjgype": "Aadhar Card",
        "nomineekjgdno": "87494726"
      }
    ]
  }
]

如何使用jarray解析这个json

2 个答案:

答案 0 :(得分:0)

创建自定义类

public class Spouseinfo
{
    public string spousename { get; set; }
    public string spousekycidtype { get; set; }
    public string spousekycidno { get; set; }
    public string spousedob { get; set; }
}

public class Nominejeinfo
{
    public string nomijjjjgjme { get; set; }
    public string nomineekjgype { get; set; }
    public string nomineekjgdno { get; set; }
}

public class RootObject
{
    public string religion { get; set; }
    public string otherreligionidtype { get; set; }
    public string caste { get; set; }
    public string personwithdhhhy { get; set; }
    public string purpogfgggan { get; set; }
    public string typeongghance { get; set; }
    public string proposehhhgenure { get; set; }
    public string repajjgjequency { get; set; }
    public string sajgjgank { get; set; }
    public string savijgjcno { get; set; }
    public string mjjggde { get; set; }
    public string ifggde { get; set; }
    public List<Spouseinfo> spouseinfo { get; set; }
    public List<Nominejeinfo> nominejeinfo { get; set; }
}

我通常会添加newtonsoft Package

var deserializedlist = JsonConvert.DeserializeObject<RootObject>(yourjsonString);

修改 阅读配偶名称

for(int i=0;i<deserializedlist.spouseinfo.Count();i++)
{
 string tempspousename=deserializedlist.spouseinfo[i].spousename;
}

答案 1 :(得分:0)

因为你坚持使用JArray由于某种原因这个代码应该是技巧

   var json = JArray.Parse(s);
        foreach (JObject obj in json.Children<JObject>())
        {
            foreach (JProperty property in obj.Properties())
            {

                if (property.Value as JArray!=null)
                {
                    var jsonArray = JArray.Parse(property.Value.ToString());
                     Console.WriteLine(property.Name + ":");
                    foreach(JObject o in jsonArray.Children<JObject>())
                        foreach(JProperty p in o.Properties())
                        {

                            Console.WriteLine(p.Name + " " + p.Value.ToString());
                        }
                }
                else
                {
                    string name = property.Name;
                    string value = property.Value.ToString();
                    Console.WriteLine(name + " " + value);
                }
            }
        }