Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#转换字典<;对象、列表<;对象>&燃气轮机;到JSON_C#_Angularjs_Json_Dictionary - Fatal编程技术网

c#转换字典<;对象、列表<;对象>&燃气轮机;到JSON

c#转换字典<;对象、列表<;对象>&燃气轮机;到JSON,c#,angularjs,json,dictionary,C#,Angularjs,Json,Dictionary,我在数据库中有两个具有一对多关系的表。州有许多城市。我从数据库中获取这些数据,并将它们的关系转换为c中对象的逻辑 将Dictionary转换为JSON并显示,然后使用AngularJs [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public static Dictionary<State, List<City>> GetCitiesState() { List<State

我在
数据库
中有两个具有一对多关系的表。州有许多城市。我从数据库中获取这些数据,并将它们的
关系
转换为
c
中对象的逻辑

Dictionary
转换为JSON并显示,然后使用
AngularJs

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static Dictionary<State, List<City>> GetCitiesState()
{
    List<State> stateList = new List<State>();
    stateList = StateList();
    List<City> cityList = new List<City>();
    cityList = CityList();
    List<City> currentList = new List<City>();

    Dictionary<State, List<City>> dictionary = new Dictionary<State, List<City>>();
    bool found = false;
    foreach (State state in stateList)
    {
        foreach (City city in cityList)
        {
            if (state.Id == city.StateId)
            {
                found = true;
                currentList.Add(city);
            }
        }
        if (found)
        {
            dictionary.Add(state, currentList);
        }
    }
    return dictionary;
}
谢谢大家!

虽然在C语言中可以使用
字典
,但声明:

序列化字典时,字典的键将转换为字符串并用作JSON对象属性名。为键编写的字符串可以通过为键类型重写ToString()或实现TypeConverter进行自定义。当反序列化字典时,TypeConverter还支持再次将自定义字符串转换回

本质上,它是说不能将状态用作键。你有几个选择

  • 将州和城市作为
    字典发送给您,让前端为您处理好关系
  • 创建一个处理后端关系的匿名类
  • 这两种方法都记录在这里

    方法1:作为
    字典发送

    [WebMethod]
    public static string GetCitiesState()
    {
        Dictionary<string, object> result = new Dictionary<string, object>();
    
        List<State> states = StateList();
        List<City> cities = CityList();
    
        result.Add("states", states);
        result.Add("cities", cities);
    
        return new JavaScriptSerializer().Serialize(result);
    }
    
    [WebMethod]
    公共静态字符串GetCitiesState()
    {
    字典结果=新字典();
    列表状态=状态列表();
    列表城市=城市列表();
    结果.添加(“国家”,国家);
    结果:添加(“城市”,城市);
    返回新的JavaScriptSerializer().Serialize(结果);
    }
    
    方法2:作为
    列表发送

    [WebMethod]
    public static string GetCitiesState()
    {
        Dictionary<string, object> result = new Dictionary<string, object>();
    
        List<State> states = StateList();
        List<City> cities = CityList();
    
        result.Add("states", states);
        result.Add("cities", cities);
    
        return new JavaScriptSerializer().Serialize(result);
    }
    
    基本思想是创建一个匿名类型对象列表(或者您可以创建自己的类)。您可以调用
    newlist()

    [WebMethod]
    公共静态字符串GetCitiesState()
    {
    var result=新列表();
    //获取状态列表和城市列表
    //等等…你的代码在这里等等。。。
    布尔值=false;
    foreach(状态列表中的状态)
    {
    var currentState=new{
    id=state.id,
    state=state.state,
    stateName=state.Name,
    城市=新列表()
    };
    foreach(城市列表中的城市)
    {
    if(city.StateId==state.Id)
    {
    发现=真;
    currentState.cities.Add(城市);
    }
    }
    如果(找到)
    { 
    //将其添加到运行状态列表中,仅包含城市的状态。
    //在这种情况下,这是没有意义的,因为每个州都有
    //城市,但额外的信息
    结果。添加(当前状态);
    }
    }
    返回新的JavaScriptSerializer().Serialize(结果);
    }
    
    该文本中没有问号。代码是否编译?如果没有,错误信息是什么?它是否编译但失败?如果是,错误信息是什么?它是否运行并返回JSON而不是您所期望的?告诉我们输出是什么以及您的期望。可能是@ThomasWeller的重复,我编辑了这个问题。谢谢你提醒我这些额外的信息。Json格式正是我想要的。如果我有模型,但我没有,这会更容易,这也是我使用Dictionary的原因。也许正如你所说,它是一个无效的字典。我正在尝试使用字典。@FationHadri请参见编辑。将对象作为键是一个非常好的解决方案。非常感谢您的想法!
    [WebMethod]
    public static string GetCitiesState()
    {
        var result = new List<object>();
    
        //get stateList and cityList
        //etc... your code here etc...
        boolean found=false;
        foreach(State state in stateList)
        {
            var currentState = new {
                id = state.Id,
                state = state.State,
                stateName = state.Name,
                cities = new List<City>()
            };
    
            foreach(City city in cityList)
            {
                if(city.StateId == state.Id)
                {
                    found=true;
                    currentState.cities.Add(city);
                }
            }
    
            if(found )
            { 
            //add it to the running state list,only states that have cities.
            //on this situation it does not make sense because every states have  
            //cities but for extra info                                                                                              
            result.Add(currentState);
            }
        }
    
        return new JavaScriptSerializer().Serialize(result);
    }