Чтение файла Json в ASP.NET с использованием C#C#

Место общения программистов C#
Ответить
Anonymous
 Чтение файла Json в ASP.NET с использованием C#

Сообщение Anonymous »

У меня есть файл 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": "test@gmail.com",
"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 { get; set; }
public string customerId { get; set; }
public string paymentbank { get; set; }
public string secdeposit { get; set; }
public List 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 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"]);
}

}
Я не получаю никакой пользы

Подробнее здесь: https://stackoverflow.com/questions/783 ... ng-c-sharp
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C#»