C# Loop trought字典<;字符串,对象>;仅适用于c中的键#

C# Loop trought字典<;字符串,对象>;仅适用于c中的键#,c#,object,dictionary,foreach,key,C#,Object,Dictionary,Foreach,Key,我有一个字符串和对象的字典,我通过反序列化这个json答案获得: {"labels":[{"id":"1","descrizione":"Etichetta interna","tipo":"0","template_file":"et_int.txt"},{"id":"2","descrizione":"Etichetta esterna","tipo":"1","template_file":"et_ext.txt"}],"0":200,"error":false,"status":200}

我有一个字符串和对象的字典,我通过反序列化这个json答案获得:

{"labels":[{"id":"1","descrizione":"Etichetta interna","tipo":"0","template_file":"et_int.txt"},{"id":"2","descrizione":"Etichetta esterna","tipo":"1","template_file":"et_ext.txt"}],"0":200,"error":false,"status":200}
使用代码:

var labels = new JavaScriptSerializer().Deserialize<Dictionary<string, object>>(json);   
但我有一个错误:

CS1579: foreach statement cannot operate on variables of type 'object' because 'object' does not contain a public definition for 'GetEnumerator'.

解决了用类替换词典的问题,谢谢你

你需要循环浏览你的词典

    foreach(KeyValuePair<string, Object> entry in labels)
     {
          // do something with entry.Value or entry.Key
     }
foreach(标签中的KeyValuePair条目)
{
//对entry.Value或entry.Key执行某些操作
}

一旦你开始循环通过它,你将获得对键和值的访问权。因为您对entry.value感兴趣,所以可以轻松地对其进行操作。当前,您的字典值是一种没有枚举数的对象类型

您需要在字典中循环

    foreach(KeyValuePair<string, Object> entry in labels)
     {
          // do something with entry.Value or entry.Key
     }
foreach(标签中的KeyValuePair条目)
{
//对entry.Value或entry.Key执行某些操作
}

一旦你开始循环通过它,你将获得对键和值的访问权。因为您对entry.value感兴趣,所以可以轻松地对其进行操作。当前,您的字典值是一种没有枚举数的对象类型

您需要在字典中循环

    foreach(KeyValuePair<string, Object> entry in labels)
     {
          // do something with entry.Value or entry.Key
     }
foreach(标签中的KeyValuePair条目)
{
//对entry.Value或entry.Key执行某些操作
}

一旦你开始循环通过它,你将获得对键和值的访问权。因为您对entry.value感兴趣,所以可以轻松地对其进行操作。当前,您的字典值是一种没有枚举数的对象类型

您需要在字典中循环

    foreach(KeyValuePair<string, Object> entry in labels)
     {
          // do something with entry.Value or entry.Key
     }
foreach(标签中的KeyValuePair条目)
{
//对entry.Value或entry.Key执行某些操作
}
一旦你开始循环通过它,你将获得对键和值的访问权。因为您对entry.value感兴趣,所以可以轻松地对其进行操作。当前,您的字典值是一种没有枚举器的对象类型。可能的解决方案是:

static void Main(string[] args) {

    string json = @"{'labels':[{'id':'1','descrizione':'Etichetta interna','tipo':'0','template_file':'et_int.txt'},{'id':'2','descrizione':'Etichetta esterna','tipo':'1','template_file':'et_ext.txt'}],'0':200,'error':false,'status':200}";
    var labels = new JavaScriptSerializer().Deserialize<Dictionary<string, object>>(json);
    IEnumerable inner_labels = labels["labels"] as IEnumerable;
    if (inner_labels != null) {
        foreach (var outer in inner_labels) { 
            Console.WriteLine(outer);
        }
    }

}
static void Main(字符串[]args){
字符串json=@“{'labels':[{'id':'1','descripione':'Etichetta interna','tipo':'0','template_file':'et_int.txt'},{'id':'2','descripione':'Etichetta estena','tipo','template_file':'et_ext.txt'},'0','200,'error':false,'status':200}”;
var labels=new JavaScriptSerializer()。反序列化(json);
IEnumerable internal_labels=标签[“标签”]作为IEnumerable;
if(内部标签!=null){
foreach(内部标签中的外部变量){
控制台写入线(外部);
}
}
}
否则,您可以使用反序列化信息创建一个类,并指示反序列化器将json字符串反序列化为该类型:

using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using System.Xml.Serialization;

[Serializable]
public class JsonData {

    [XmlElement("labels")]
    public List<JsonLabel> labels { get; set; }

    [XmlElement("0")]
    public int zero { get; set; }

    [XmlElement("error")]
    public bool error { get; set; }

    [XmlElement("status")]
    public int status { get; set; }


}

[Serializable]
public class JsonLabel {
    [XmlElement("id")]
    public int id { get; set; }

    [XmlElement("descrizione")]
    public string descrizione { get; set; }

    [XmlElement("tipo")]
    public int tipo { get; set; }

    [XmlElement("template_file")]
    public string template_file { get; set; }

}

class Program {


    static void Main(string[] args) {
        string json = @"your json string here...";
        var jsonData = new JavaScriptSerializer().Deserialize<JsonData>(json);
        foreach (var label in jsonData.labels) {
            Console.WriteLine(label.id);
        }
    }

}
使用系统;
使用System.Collections.Generic;
使用System.Web.Script.Serialization;
使用System.Xml.Serialization;
[可序列化]
公共类JsonData{
[XmlElement(“标签”)]
公共列表标签{get;set;}
[XmlElement(“0”)]
公共整数零{get;set;}
[XmlElement(“错误”)]
公共布尔错误{get;set;}
[XmlElement(“状态”)]
公共int状态{get;set;}
}
[可序列化]
公共类JsonLabel{
[XmlElement(“id”)]
公共int id{get;set;}
[XmlElement(“描述”)]
公共字符串描述符{get;set;}
[XmlElement(“tipo”)]
公共int tipo{get;set;}
[XmlElement(“模板文件”)]
公共字符串模板_文件{get;set;}
}
班级计划{
静态void Main(字符串[]参数){
字符串json=@“您的json字符串在此…”;
var jsonData=new JavaScriptSerializer()。反序列化(json);
foreach(jsonData.labels中的变量标签){
Console.WriteLine(label.id);
}
}
}
可能的解决方案:

static void Main(string[] args) {

    string json = @"{'labels':[{'id':'1','descrizione':'Etichetta interna','tipo':'0','template_file':'et_int.txt'},{'id':'2','descrizione':'Etichetta esterna','tipo':'1','template_file':'et_ext.txt'}],'0':200,'error':false,'status':200}";
    var labels = new JavaScriptSerializer().Deserialize<Dictionary<string, object>>(json);
    IEnumerable inner_labels = labels["labels"] as IEnumerable;
    if (inner_labels != null) {
        foreach (var outer in inner_labels) { 
            Console.WriteLine(outer);
        }
    }

}
static void Main(字符串[]args){
字符串json=@“{'labels':[{'id':'1','descripione':'Etichetta interna','tipo':'0','template_file':'et_int.txt'},{'id':'2','descripione':'Etichetta estena','tipo','template_file':'et_ext.txt'},'0','200,'error':false,'status':200}”;
var labels=new JavaScriptSerializer()。反序列化(json);
IEnumerable internal_labels=标签[“标签”]作为IEnumerable;
if(内部标签!=null){
foreach(内部标签中的外部变量){
控制台写入线(外部);
}
}
}
否则,您可以使用反序列化信息创建一个类,并指示反序列化器将json字符串反序列化为该类型:

using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using System.Xml.Serialization;

[Serializable]
public class JsonData {

    [XmlElement("labels")]
    public List<JsonLabel> labels { get; set; }

    [XmlElement("0")]
    public int zero { get; set; }

    [XmlElement("error")]
    public bool error { get; set; }

    [XmlElement("status")]
    public int status { get; set; }


}

[Serializable]
public class JsonLabel {
    [XmlElement("id")]
    public int id { get; set; }

    [XmlElement("descrizione")]
    public string descrizione { get; set; }

    [XmlElement("tipo")]
    public int tipo { get; set; }

    [XmlElement("template_file")]
    public string template_file { get; set; }

}

class Program {


    static void Main(string[] args) {
        string json = @"your json string here...";
        var jsonData = new JavaScriptSerializer().Deserialize<JsonData>(json);
        foreach (var label in jsonData.labels) {
            Console.WriteLine(label.id);
        }
    }

}
使用系统;
使用System.Collections.Generic;
使用System.Web.Script.Serialization;
使用System.Xml.Serialization;
[可序列化]
公共类JsonData{
[XmlElement(“标签”)]
公共列表标签{get;set;}
[XmlElement(“0”)]
公共整数零{get;set;}
[XmlElement(“错误”)]
公共布尔错误{get;set;}
[XmlElement(“状态”)]
公共int状态{get;set;}
}
[可序列化]
公共类JsonLabel{
[XmlElement(“id”)]
公共int id{get;set;}
[XmlElement(“描述”)]
公共字符串描述符{get;set;}
[XmlElement(“tipo”)]
公共int tipo{get;set;}
[XmlElement(“模板文件”)]
公共字符串模板_文件{get;set;}
}
班级计划{
静态void Main(字符串[]参数){
字符串json=@“您的json字符串在此…”;
var jsonData=new JavaScriptSerializer()。反序列化(json);
foreach(jsonData.labels中的变量标签){
Console.WriteLine(label.id);
}
}
}
可能的解决方案:

static void Main(string[] args) {

    string json = @"{'labels':[{'id':'1','descrizione':'Etichetta interna','tipo':'0','template_file':'et_int.txt'},{'id':'2','descrizione':'Etichetta esterna','tipo':'1','template_file':'et_ext.txt'}],'0':200,'error':false,'status':200}";
    var labels = new JavaScriptSerializer().Deserialize<Dictionary<string, object>>(json);
    IEnumerable inner_labels = labels["labels"] as IEnumerable;
    if (inner_labels != null) {
        foreach (var outer in inner_labels) { 
            Console.WriteLine(outer);
        }
    }

}
static void Main(字符串[]args){
字符串json=@“{'labels':[{'id':'1','descripione':'Etichetta interna','tipo':'0','template_file':'et_int.txt'},{'id':'2','descripione':'Etichetta estena','tipo','template_file':'et_ext.txt'},'0','200,'error':false,'status':200}”;
var labels=new JavaScriptSerializer()。反序列化(json);
IEnumerable internal_labels=标签[“标签”]作为IEnumerable;
if(内部标签!=null){
foreach(内部标签中的外部变量){
控制台写入