我正在尝试从C#中的以下JSON获取所有price
和type
。有人可以帮我吗?
{
"status": 200,
"data": {
"21": {
"cheapest_room": {
"website": {},
"corp": {
"price": 3174.08,
"type": "MAPLE"
}
},
"availability": 6,
"tcr_multiplier": 1
},
"756": {
"cheapest_room": {
"website": {},
"corp": {}
},
"availability": false
},
"1070": {
"cheapest_room": {
"website": {},
"corp": {
"price": 2808.05,
"type": "OAK"
}
},
"availability": 1,
"tcr_multiplier": 1
}
}
}
答案 0 :(得分:0)
这是一个完整的示例
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace ConsoleApp1
{
static class Program
{
static async Task Main( string[] args )
{
var endpoint = new RoomEndpoint( new FakeJsonProvider() );
var response = await endpoint.GetFooAsync();
var mydata = response.Data
.Where( e => e.Value.CheapestRoom?.Corp?.Price != null )
.Select( e => new { e.Key, Price = e.Value.CheapestRoom.Corp.Price.Value, Type = e.Value.CheapestRoom.Corp.Type } )
.ToList();
foreach (var item in mydata)
{
Console.WriteLine( "{0}-{1}: {2}", item.Key, item.Type, item.Price );
}
}
}
interface IJsonProvider
{
Task<string> GetAsync( string uri );
}
class RoomEndpoint
{
private readonly IJsonProvider _jsonProvider;
public RoomEndpoint( IJsonProvider jsonProvider )
{
_jsonProvider = jsonProvider ?? throw new ArgumentNullException( nameof( jsonProvider ) );
}
public async Task<FooResponseData> GetFooAsync()
{
var json = await _jsonProvider.GetAsync( "foo" ).ConfigureAwait( false );
return JsonConvert.DeserializeObject<FooResponseData>( json );
}
}
class FakeJsonProvider : IJsonProvider
{
public Task<string> GetAsync( string uri )
{
string json = null;
switch (uri)
{
case "foo":
json = json = Properties.Resources.FooResponseJson; // contains the given Json Response
break;
default:
throw new InvalidOperationException();
}
return Task.FromResult( json );
}
}
class CorpData
{
[JsonProperty( "price" )]
public decimal? Price { get; set; }
[JsonProperty( "type" )]
public string Type { get; set; }
}
class WebsiteData
{
}
class RoomData
{
[JsonProperty( "corp" )]
public CorpData Corp { get; set; }
[JsonProperty( "website" )]
public WebsiteData Website { get; set; }
}
class KeyData
{
[JsonProperty( "cheapest_room" )]
public RoomData CheapestRoom { get; set; }
[JsonProperty( "availibility" )]
public int? Availability { get; set; }
[JsonProperty( "tcr_multiplier" )]
public int? TcrMultiplier { get; set; }
}
class FooResponseData
{
[JsonProperty( "status" )]
public int Status { get; set; }
[JsonProperty( "data" )]
public IDictionary<string, KeyData> Data { get; set; }
}
}
答案 1 :(得分:0)
这是我使用JSON.Net的解决方案,它将生成hotelId和房价的字典。
JObject dataJson = JObject.Parse(json);
Dictionary<string, double> prices = dataJson["data"]
.Children<JProperty>()
.ToDictionary(k => k.Name,
v => Convert.ToDouble(v.Value["cheapest_room"]["corp"]["price"] ?? -1));
答案 2 :(得分:0)
您可以使用JSON Path提供的NewtonSoft's JSON,例如:
// load & parse the json
var jo = JObject.Parse(File.ReadAllText("test.json"));
// select all 'corp' objects recursively anywhere in the doc
foreach (var tok in jo.SelectTokens("..corp"))
{
// extract information (note some of your corp objects don't contain anything)
var price = tok["price"]?.Value<double>();
var type = tok["type"]?.Value<string>();
}