简体   繁体   English

C#中的JSON解析

[英]JSON Parse in C#

What is wrong with this code? 此代码有什么问题?

JSON JSON格式

cities: [
{
    city: {
        id: 1,
        name: "A.S.Peta",
        status: "Active"
    }
},..............

C# Code C#代码

public class Cities
{
    public City[] cities;    
}

public class City
{
    public int id; //{ get; set; }
    public string name; //{ get; set; }
    public string status; //{ get; set; }
}

//De-Serialization
var jsSerialize = new JavaScriptSerializer();
var cities = jsSerialize.Deserialize<Cities>(result);

Not populating internal object City. 没有填充内部对象City。 but showing collection with all records. 但显示所有记录的集合。 Any Idea? 任何想法?

The "inner" city in your json object is adding a nested object within the array. json对象中的“内部” city在数组内添加了一个嵌套对象。

Try this json code : 试试这个json代码:

{
    "cities": [
    {  
        "id": 1,
        "name": "A.S.Peta",
        "status": "Active"
    },
    {  
        "id": 2,
        "name": "Strasbourg",
        "status": "Active"
    }
    ]
}

If you need to stick to your originial json structure, you can try this c# code: 如果您需要坚持使用原始的json结构,则可以尝试以下C#代码:

public class City2
{
    public int id { get; set; }
    public string name { get; set; }
    public string status { get; set; }
}

public class City
{
    public City2 city { get; set; }
}

public class RootObject
{
    public List<City> cities { get; set; }
}

This code has been automatically generated by this very useful web tool: json2C# 此代码已通过此非常有用的Web工具自动生成: json2C#

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM