在ASP.Net MVC JSON转换为C#类 [英] Converting JSON to C# classes in ASP.Net MVC

查看:112
本文介绍了在ASP.Net MVC JSON转换为C#类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个API - 预期输出(用于第三方)在其网站上如低于JSON:

I'm trying to write an API - the expected output (for a 3rd party) as shown on their website is the JSON below:

{
"api_version" : 4 ,
"hotel_ids" : [97497],   
"hotels" :
    [
        {
            "hotel_id": 97497,
            "room_types":
                {
                    "Fenway Room":
                        {
                            "url": "someurlhere",
                            "price": 178.50, 
                            "room_code": "SINGLE"                               
                        }
                }
        }
    ]
}

我使用的在线工具: http://json2csharp.com/

它给了我下面的类:

public class FenwayRoom
{
public string url { get; set; }
public double price { get; set; }
public string room_code { get; set; }
}

public class RoomTypes
{
public FenwayRoom __invalid_name__Fenway Room { get; set; }
}

public class Hotel
{
public int hotel_id { get; set; }
public RoomTypes room_types { get; set; }
}

public class RootObject
{
public int api_version { get; set; }
public List<int> hotel_ids { get; set; }
public List<Hotel> hotels { get; set; }
}

您可以看到RoomTypes:

You can see in RoomTypes:

FenwayRoom __invalid_name__Fenway室{搞定;组; }

我想知道如果他们对JSON规范是错误的,或者是有我创建类,返回他们所期望的JSON的方法吗?

I'm wondering if their spec for the JSON is wrong, or is there a way for me to create the classes to return the JSON they are expecting?

在这个例子中,我相信房型芬威室是一个变量 - 所以我不认为它也可以是一个类名。但它可能只是我不知道如何创建一个类是这样的。

In the example, I believe the room type "Fenway Room" is a variable - so I don't think it can also be a class name. But it may just be that I don't know how to create a class like this.

我只是想不通了芬威室 - 我认为需要有这样的:房间名称:芬威室 - 但也许有定义的类,以便它不'的另一种方式吨需要一个标签房间名称。

I just can't figure out the "Fenway Room" - which I think needs to have something like: "Room Name":"Fenway Room" - but maybe there is another way of defining the classes so that it doesn't need a label "Room Name".

我想AP preciate在确认是否可以创建类来匹配他们的JSON任何帮助。

I'd appreciate any help in confirming if it is possible to create classes to match against their JSON.

谢谢,马克

推荐答案

在使用Json.NET输出

When using Json.NET the output

"room_types":
{
    "Fenway Room":
    {
        "url": "someurlhere",
        "price": 178.50, 
        "room_code": "SINGLE"                               
        }
    }
}

看起来确实,你会得到一个序列什么词典

look exactly what you would get serializing a Dictionary.

您的类更改

public class Room
{
    public string url { get; set; }
    public double price { get; set; }
    public string room_code { get; set; }
}

public class Hotel
{
    public int hotel_id { get; set; }
    public Dictionary<string, Room> room_types { get; set; }
}

public class RootObject
{
    public int api_version { get; set; }
    public List<int> hotel_ids { get; set; }
    public List<Hotel> hotels { get; set; }
}

这是pdicated使用Json.NET,它给你的字典这种序列化/反序列化这种格式自由所有$ P $。您可以使用.NET Framework序列化做到这一点,但你需要做额外的工作。

This is all predicated on using Json.NET, which gives you this serialization/deserialization of dictionaries in this format for free. You can do this with the .NET framework serializer, but you need to do extra work.

这篇关于在ASP.Net MVC JSON转换为C#类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆