使用C#读取ASP.NET中的Json文件

问题描述 投票:0回答:1

我有一个 JSON 文件,请告诉我如何读取该文件。我创建了一个类文件,想要在这些类属性中保存 JSON 数据。

{
  "header": {
    "hid": "95845-5bhj-j908-o987-hhg5665",
    "datestamp": "2024-01-21"
  },
  "body": [
    {
      "hotelbooking": [
        {
          "Customer": {
            "Bookings": {
              "Profile": {
                "name": "Test",
                "dob": "1989-01-01",
                "mobile": "+919999999999",
                "address": "C-366,Saket,Delhi,INDIA,110044",
                "email": "[email protected]",
                "id": "GH2673677"
              },
              "roomtype": "DELUXE POOL VIEW"
            }
          },
          "BookingStatus": {
            "Confirm": {
              "amount": 4800
            },
            "Pax": "4",
            "currency": "THB",
            "bookDateTime": "2024-01-21T21:10:54Z",
            "address": "PATONG",
              "days": "4",
            "paymentstatus": "CONFIRM"
          },
          "RoomDetails": {
            "Rooms": [
              {
                "roomtype": "DELUXE GARDEN VIEW",
                "pricepernight": 1000,
                "currency": "THB",
                "refid": "0032514569"
              },
              {
                "roomtype": "DELUXE POOL VIEW",
                "pricepernight": 1200,
                "currency": "THB",
                "refid": "0069584214"
              },
              {
                "roomtype": "DELUXE SEA VIEW",
                "pricepernight": 1300,
                "currency": "THB",
                "refid": "0098541239"
              },
              
              {
                "roomtype": "SUITE POOL VIEW",
                "pricepernight": 2000,
                "currency": "THB",
                "refid": "0036521456"
              },
              {
                "roomtype": "SUITE POOL ACCESS",
                "pricepernight": 3000,
                "currency": "THB",
                "refid": "002651425"
              },
              {
                "roomtype": "PRESEDENTIAL SUITE INFINITY POOL",
                "pricepernight": 6000,
                "currency": "THB",
                "refid": "006985321"
              }
             ],
            "bookstartDate": "2024-04-09",
            "bookendDate": "2024-04-13"
          },
          "paymentmode": "cc",
          "creditcardNumber": "XXXXXXXXXXXX8976",
          "paymentRef": "bhgd73838-jdf73738"
        }
      ],
      "customerId": "yryu78876",
      "paymentbank": "DBS Bank",
      "secdeposit": "No",
      "OtherInfo": [
        {
          "ExtraBed": "No",
            "Breakfast": "Yes",
          "RefNo": "0069584214"
        }
      ]
    }
  ]
}

我正在使用以下代码读取JSON,如何读取JSON的节点?

我想读取JSON文件中的所有节点。我已经创建了所有节点的属性,同时将房间类型保存在列表中。

这是我班级中的代码:

 public class Body
    {
        public List<Hotelbooking> hotelbooking { get; set; }
        public string customerId { get; set; }
        public string paymentbank { get; set; }
        public string secdeposit { get; set; }
        public List<OtherInfo> OtherInfo { get; set; }
    }
 public class Bookings
{
    public Profile Profile { get; set; }
    public string roomtype { get; set; }
}

public class BookingStatus
{
    public Confirm Confirm { get; set; }
    public string Pax { get; set; }
    public string currency { get; set; }
    public DateTime bookDateTime { get; set; }
    public string address { get; set; }
    public string days { get; set; }
    public string paymentstatus { get; set; }
}

public class Confirm
{
    public int amount { get; set; }
}

public class Customer
{
    public Bookings Bookings { get; set; }
}

public class Header
{
    public string hid { get; set; }
    public string datestamp { get; set; }
}

public class Hotelbooking
{
    public Customer Customer { get; set; }
    public BookingStatus BookingStatus { get; set; }
    public RoomDetails RoomDetails { get; set; }
    public string paymentmode { get; set; }
    public string creditcardNumber { get; set; }
    public string paymentRef { get; set; }
}

public class OtherInfo
{
    public string ExtraBed { get; set; }
    public string Breakfast { get; set; }
    public string RefNo { get; set; }
}

public class Profile
{
    public string name { get; set; }
    public string dob { get; set; }
    public string mobile { get; set; }
    public string address { get; set; }
    public string email { get; set; }
    public string id { get; set; }
}

public class Room
{
    public string roomtype { get; set; }
    public int pricepernight { get; set; }
    public string currency { get; set; }
    public string refid { get; set; }
}

public class RoomDetails
{
    public List<Room> Rooms { get; set; }
    public string bookstartDate { get; set; }
    public string bookendDate { get; set; }
}

这是我读取 JSON 文件的代码:

 string jsonFile = @"E:\hotel.json";
    var json = File.ReadAllText(jsonFile);

    var jsonObject = JObject.Parse(json);
JArray hotelbodyArrary = (JArray)jsonObject["body"];

if (hotelbodyArrary != null)
{
    Body body = new Body();
    Hotelbooking ho = new Hotelbooking();
    foreach (var item in hotelbodyArrary)
    {
        
        ho.paymentmode = Convert.ToString(item["paymentmode"]);
        ho.creditcardNumber = Convert.ToString(item["creditcardNumber"]);
    }

}

我没有得到任何价值

c# json visual-studio-2022
1个回答
0
投票

您的

body
数组由多个带有
hotelbooking
数组的对象组成。您需要迭代
hotelbooking
数组中的每个对象以获得
paymentmode
creditcardNumber
值。

Body body = new Body();
body.hotelbooking = new List<Hotelbooking>();

Hotelbooking ho = new Hotelbooking();
foreach (var item in hotelbodyArrary)
{
    JArray hotelBookingJArray = JArray.FromObject(item["hotelbooking"]);
    foreach (var hotelJObj in hotelBookingJArray)
    {
        ho.paymentmode = Convert.ToString(hotelJObj["paymentmode"]);
        ho.creditcardNumber = Convert.ToString(hotelJObj["creditcardNumber"]);

        body.hotelbooking.Add(ho);
    }
}

这将使将其反序列化为类变得更容易,并且您会错过

Root
类。

Root root = JsonConvert.DeserializeObject<Root>(json);

public class Root
{
    [JsonProperty("header")]
    public Header Header { get; set; }

    [JsonProperty("body")]
    public List<Body> Body { get; set; }
}
© www.soinside.com 2019 - 2024. All rights reserved.