在c#中循环json数组

本文关键字:json 数组 循环 | 更新日期: 2023-09-27 18:14:27

我有一个json字符串,如

{"objectType" : "Subscriber", "objectList":[{"firstName":"name1","email":"email@example.com","address":"exampleAddress"},{"firstName":"name2","email":"email2@example.com","address":"exampleAddress2"}]}

我需要在c#代码中解析它。我试过了,

JavaScriptSerializer json_serializer = new JavaScriptSerializer();
object routes_list = json_serializer.DeserializeObject(myjson here);

但是我不能循环通过"objectList"数组。怎样才能做到呢?

在c#中循环json数组

var jsonObj = new JavaScriptSerializer().Deserialize<RootObj>(json);
foreach (var obj in jsonObj.objectList)
{
    Console.WriteLine(obj.address);
}

public class ObjectList
{
    public string firstName { get; set; }
    public string email { get; set; }
    public string address { get; set; }
}
public class RootObj
{
    public string objectType { get; set; }
    public List<ObjectList> objectList { get; set; }
}

提示:你可以使用这个网站将json字符串转换为c#类

编辑

使用Json。净

dynamic jsonObj = JsonConvert.DeserializeObject(json);
foreach (var obj in jsonObj.objectList)
{
    Console.WriteLine(obj.address);
}
var routes_list = (Dictionary<string, object>)json_serializer.DeserializeObject(myjson);
foreach (var record in routes_list)
{
    Console.WriteLine(record);
}

这对我来说很有效,基本上将JSON转换为YAML

    string JSONDeserialized {get; set;}
    public int indentLevel;
    private bool JSONDictionarytoYAML(Dictionary<string, object> dict)
    {
        bool bSuccess = false;
        indentLevel++;
        foreach (string strKey in dict.Keys)
        {
            string strOutput = "".PadLeft(indentLevel * 3) + strKey + ":";
            JSONDeserialized+="'r'n" + strOutput;
            object o = dict[strKey];
            if (o is Dictionary<string, object>)
            {
                JSONDictionarytoYAML((Dictionary<string, object>)o);
            }
            else if (o is ArrayList)
            {
                foreach (object oChild in ((ArrayList)o))
                {
                    if (oChild is string)
                    {
                        strOutput = ((string)oChild);
                        JSONDeserialized += strOutput + ",";
                    }
                    else if (oChild is Dictionary<string, object>)
                    {
                        JSONDictionarytoYAML((Dictionary<string, object>)oChild);
                        JSONDeserialized += "'r'n";  
                    }
                }
            }
            else
            {
                strOutput = o.ToString();
                JSONDeserialized += strOutput;
            }
        }
        indentLevel--;
        return bSuccess;
    }
使用

        Dictionary<string, object> JSONDic = new Dictionary<string, object>();
        JavaScriptSerializer js = new JavaScriptSerializer();
          try {
            JSONDic = js.Deserialize<Dictionary<string, object>>(inString);
            JSONDeserialized = "";
            indentLevel = 0;
            DisplayDictionary(JSONDic); 
            return JSONDeserialized;
        }
        catch (Exception)
        {
            return "Could not parse input JSON string";
        }