鉴于以下json:
[ {"id":"123", ... "data":[{"key1":"val1"}, {"key2":"val2"}], ...}, ... ]
是更大树的一部分,如何将“data”属性反序列化为:
List<MyCustomClass> Data { get; set; }
或
List<KeyValuePair> Data { get; set; }
或
Dictionary<string, string> Data { get; set; }
使用Json.NET?两个版本都可以(我更喜欢List of MyCustomClass)。我已经有一个包含其他属性的类,如下所示:
public class SomeData
{
[JsonProperty("_id")]
public string Id { get; set; }
...
public List<MyCustomClass> Data { get; set; }
}
其中“MyCustomClass”只包含两个属性(Key和Value)。我注意到有一个KeyValuePairConverter类听起来像它会做我需要的,但我无法找到如何使用它的例子。谢谢。
答案 0 :(得分:18)
最简单的方法是将键值对数组反序列化为IDictionary<string, string>
:
public class SomeData
{
public string Id { get; set; }
public IEnumerable<IDictionary<string, string>> Data { get; set; }
}
private static void Main(string[] args)
{
var json = "{ \"id\": \"123\", \"data\": [ { \"key1\": \"val1\" }, { \"key2\" : \"val2\" } ] }";
var obj = JsonConvert.DeserializeObject<SomeData>(json);
}
但是如果你需要将它反序列化到你自己的类中,它可能看起来像这样:
public class SomeData2
{
public string Id { get; set; }
public List<SomeDataPair> Data { get; set; }
}
public class SomeDataPair
{
public string Key { get; set; }
public string Value { get; set; }
}
private static void Main(string[] args)
{
var json = "{ \"id\": \"123\", \"data\": [ { \"key1\": \"val1\" }, { \"key2\" : \"val2\" } ] }";
var rawObj = JObject.Parse(json);
var obj2 = new SomeData2
{
Id = (string)rawObj["id"],
Data = new List<SomeDataPair>()
};
foreach (var item in rawObj["data"])
{
foreach (var prop in item)
{
var property = prop as JProperty;
if (property != null)
{
obj2.Data.Add(new SomeDataPair() { Key = property.Name, Value = property.Value.ToString() });
}
}
}
}
看到我说Value
是string
并且我调用了ToString()
方法,可能会有另一个复杂的类。
答案 1 :(得分:2)
我最终这样做了:
[JsonConverter(typeof(MyCustomClassConverter))]
public class MyCustomClass
{
internal class MyCustomClassConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
JObject jObject = JObject.Load(reader);
foreach (var prop in jObject)
{
return new MyCustomClass { Key = prop.Key, Value = prop.Value.ToString() };
}
return null;
}
public override bool CanConvert(Type objectType)
{
return typeof(MyCustomClass).IsAssignableFrom(objectType);
}
}
public string Key { get; set; }
public string Value { get; set; }
}
答案 2 :(得分:0)
public class Datum
{
public string key1 { get; set; }
public string key2 { get; set; }
}
public class RootObject
{
public string id { get; set; }
public List<Datum> data { get; set; }
}
我也使用了此向导json2csharp.com来生成反序列化的类
使用该
using RestSharp;
using Newtonsoft.Json;
IRestResponse restSharp= callRestGetMethodby_restSharp(api_server_url);
string jsonString= restSharp.Content;
RootObject rootObj= JsonConvert.DeserializeObject<RootObject>(jsonString);
return Json(rootObj);
如果您通过restsharp呼叫休息
public IRestResponse callRestGetMethodby_restSharp(string API_URL)
{
var client = new RestSharp.RestClient(API_URL);
var request = new RestRequest(Method.GET);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("cache-control", "no-cache");
IRestResponse response = client.Execute(request);
return response;
}
您还可以从getpostman.com工具中获得这6行restsharp
答案 3 :(得分:0)
感谢@Boo的回答,但就我而言,我需要进行一些小的调整。 这是我的JSON的样子:
{
"rates": {
"CAD": 1.5649,
"CZK": 26,118,
...
},
"base": "EUR",
"date": "2020-08-16"
}
我的DTO如下所示:
public IDictionary<string, decimal> Rates { get; set; }
public string Base { get; set; }
public DateTime Date { get; set; }
因此唯一的调整是删除IEnumerable
周围的IDictionary
。