问题描述
我有如下的 Json 字符串
I have Json string like below
{
"JsonValues":{
"id": "MyID",
"values": {
"value1":{
"id": "100",
"diaplayName": "MyValue1"
},
"value2":{
"id": "200",
"diaplayName": "MyValue2"
}
}
}
}
我想将 Json 字符串转换为下面的类
I want to convert Json string to below classes
class ValueSet
{
[JsonProperty("id")]
public string id
{
get;
set;
}
[JsonProperty("values")]
public List<Value> values
{
get;
set;
}
}
class Value
{
public string id
{
get;
set;
}
public string DiaplayName
{
get;
set;
}
}
我的反序列化代码是
JavaScriptSerializer js = new JavaScriptSerializer();
StreamReader sr = new StreamReader(@"ValueSetJsonString.txt");
string jsonString = sr.ReadToEnd();
var items = JsonConvert.DeserializeObject<ValueSet>(jsonString);
但是序列化后我得到空值,我该如何解决这个问题?
But I am getting null values after serialization, How i can solve this?
推荐答案
正如其他人已经指出的那样,您没有得到预期结果的原因是您的 JSON 与您尝试反序列化的类结构不匹配进入.您要么需要更改 JSON,要么更改您的类.由于其他人已经展示了如何更改 JSON,我将在此处采取相反的方法.
As others have already pointed out, the reason you are not getting the results you expect is because your JSON does not match the class structure that you are trying to deserialize into. You either need to change your JSON or change your classes. Since others have already shown how to change the JSON, I will take the opposite approach here.
为了匹配您在问题中发布的 JSON,您的类应定义如下.请注意,我进行了以下更改:
To match the JSON you posted in your question, your classes should be defined like those below. Notice I've made the following changes:
- 我添加了一个
Wrapper
类,对应于您的 JSON 中的外部对象. - 我将
ValueSet
类的Values
属性从List
更改为Dictionary
因为 JSON 中的; values
属性包含一个对象,而不是数组. - 我添加了一些额外的
[JsonProperty]
属性来匹配 JSON 对象中的属性名称.
- I added a
Wrapper
class corresponding to the outer object in your JSON. - I changed the
Values
property of theValueSet
class from aList<Value>
to aDictionary<string, Value>
since thevalues
property in your JSON contains an object, not an array. - I added some additional
[JsonProperty]
attributes to match the property names in your JSON objects.
类定义:
class Wrapper
{
[JsonProperty("JsonValues")]
public ValueSet ValueSet { get; set; }
}
class ValueSet
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("values")]
public Dictionary<string, Value> Values { get; set; }
}
class Value
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("diaplayName")]
public string DisplayName { get; set; }
}
您需要反序列化到 Wrapper
类,而不是 ValueSet
类.然后,您可以从 Wrapper
中获取 ValueSet
.
You need to deserialize into the Wrapper
class, not the ValueSet
class. You can then get the ValueSet
from the Wrapper
.
var valueSet = JsonConvert.DeserializeObject<Wrapper>(jsonString).ValueSet;
这是一个演示的工作程序:
Here is a working program to demonstrate:
class Program
{
static void Main(string[] args)
{
string jsonString = @"
{
""JsonValues"": {
""id"": ""MyID"",
""values"": {
""value1"": {
""id"": ""100"",
""diaplayName"": ""MyValue1""
},
""value2"": {
""id"": ""200"",
""diaplayName"": ""MyValue2""
}
}
}
}";
var valueSet = JsonConvert.DeserializeObject<Wrapper>(jsonString).ValueSet;
Console.WriteLine("id: " + valueSet.Id);
foreach (KeyValuePair<string, Value> kvp in valueSet.Values)
{
Console.WriteLine(kvp.Key + " id: " + kvp.Value.Id);
Console.WriteLine(kvp.Key + " name: " + kvp.Value.DisplayName);
}
}
}
这是输出:
id: MyID
value1 id: 100
value1 name: MyValue1
value2 id: 200
value2 name: MyValue2
这篇关于如何将Json数组转换为C#中的对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!