如何使用 Newtonsoft 在 C# .NET 中从我的 JSON 数组反序列化为字典数组,其值是我的 "Resource" 对象?
How to deserialize from my JSON array to an array of dictionaries whose values are my "Resource" objects in C# .NET using Newtonsoft?
我在尝试将我的 JSON 反序列化为 Dictionary<string, Resources>[]
时遇到以下错误:
An unhandled exception of type
'Newtonsoft.Json.JsonSerializationException' occurred in
Newtonsoft.Json.dll
Additional information: Cannot deserialize the current JSON object
(e.g. {"name":"value"}) into type
'System.Collections.Generic.Dictionary`2[System.String,MyProject.Resource][]'
because the type requires a JSON array (e.g. [1,2,3]) to deserialize
correctly.
To fix this error either change the JSON to a JSON array (e.g.
[1,2,3]) or change the deserialized type so that it is a normal .NET
type (e.g. not a primitive type like integer, not a collection type
like an array or List) that can be deserialized from a JSON object.
JsonObjectAttribute can also be added to the type to force it to
deserialize from a JSON object.
我的Program.Main():
public class Program
{
public static void Main(string[] Args)
{
var options = new Options()
{
Option1 = "foo",
Option2 = "bar",
Resources = JsonConvert.DeserializeObject<Dictionary<string, Resource>[]>(resources.json)
};
}
}
我的resources.json:
{
"global": [
{
"title": "global1",
"url": "~/global1"
},
{
"title": "global2",
"url": "~/global2"
}
],
"ACC": [
{
"title": "acc1",
"url": "~/acc1/"
},
{
"title": "acc2",
"url": "~/acc2/"
}
],
"ECC": [
{
"title": "ecc1",
"url": "~/ecc1/"
},
{
"title": "ecc2",
"url": "~/ecc2/"
}
],
"ECB": [
{
"title": "ecb1",
"url": "~/ecb1"
}
]
}
我的资源class:
public class Resource
{
public List<string> titles { get; set; }
public List<string> urls { get; set; }
}
我的选项class:
public class Options
{
public string Option1 { get; set; }
public string Option2 { get; set; }
public Dictionary<string, Resource>[] Resources { get; set; }
}
您的 Json 与您的 class 结构不匹配。
如果您想保留 class 结构,您的 json 必须如下所示:
{
"global":{
"titles":[
"global1",
"global2"
],
"urls":[
"~/global1",
"~/global2"
]
},
"acc":{
"titles":[
"acc1",
"acc2"
],
"urls":[
"~/acc1",
"~/acc2"
]
},
...
}
如果您不能影响您的 json 文件(无论出于何种原因),您的 class 结构必须如下所示:
public class Rootobject
{
public Global[] global { get; set; }
public ACC[] ACC { get; set; }
public ECC[] ECC { get; set; }
public ECB[] ECB { get; set; }
}
public class Global
{
public string title { get; set; }
public string url { get; set; }
}
public class ACC
{
public string title { get; set; }
public string url { get; set; }
}
public class ECC
{
public string title { get; set; }
public string url { get; set; }
}
public class ECB
{
public string title { get; set; }
public string url { get; set; }
}
然后你可以这样反序列化它:
Resources = JsonConvert.DeserializeObject<Rootobject>(resources.json)
下次小记:
如果您不确定 class 结构的外观(关于 json 字符串),您可以复制 json 字符串并转到:
Visual Studio -> Edit -> Paste Special -> Paste JSON as Classes
和Visual Studio将构成适当的class结构。
你的JSON不代表一个字典数组(Dictionary<string, Resource>[]
),它代表一个数组字典(Dictionary<string, Resource[]>
)。这就是您收到错误的原因。
此外,您的资源 class 需要像这样定义以匹配 JSON:
public class Resource
{
public string Title { get; set; }
public string Url { get; set; }
}
然后你可以这样反序列化:
Resources = JsonConvert.DeserializeObject<Dictionary<string, Resource[]>>(resources.json)
请在此处查看 fiddle:https://dotnetfiddle.net/8s2eQd
我在尝试将我的 JSON 反序列化为 Dictionary<string, Resources>[]
时遇到以下错误:
An unhandled exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll
Additional information: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.Dictionary`2[System.String,MyProject.Resource][]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
我的Program.Main():
public class Program
{
public static void Main(string[] Args)
{
var options = new Options()
{
Option1 = "foo",
Option2 = "bar",
Resources = JsonConvert.DeserializeObject<Dictionary<string, Resource>[]>(resources.json)
};
}
}
我的resources.json:
{
"global": [
{
"title": "global1",
"url": "~/global1"
},
{
"title": "global2",
"url": "~/global2"
}
],
"ACC": [
{
"title": "acc1",
"url": "~/acc1/"
},
{
"title": "acc2",
"url": "~/acc2/"
}
],
"ECC": [
{
"title": "ecc1",
"url": "~/ecc1/"
},
{
"title": "ecc2",
"url": "~/ecc2/"
}
],
"ECB": [
{
"title": "ecb1",
"url": "~/ecb1"
}
]
}
我的资源class:
public class Resource
{
public List<string> titles { get; set; }
public List<string> urls { get; set; }
}
我的选项class:
public class Options
{
public string Option1 { get; set; }
public string Option2 { get; set; }
public Dictionary<string, Resource>[] Resources { get; set; }
}
您的 Json 与您的 class 结构不匹配。
如果您想保留 class 结构,您的 json 必须如下所示:
{
"global":{
"titles":[
"global1",
"global2"
],
"urls":[
"~/global1",
"~/global2"
]
},
"acc":{
"titles":[
"acc1",
"acc2"
],
"urls":[
"~/acc1",
"~/acc2"
]
},
...
}
如果您不能影响您的 json 文件(无论出于何种原因),您的 class 结构必须如下所示:
public class Rootobject
{
public Global[] global { get; set; }
public ACC[] ACC { get; set; }
public ECC[] ECC { get; set; }
public ECB[] ECB { get; set; }
}
public class Global
{
public string title { get; set; }
public string url { get; set; }
}
public class ACC
{
public string title { get; set; }
public string url { get; set; }
}
public class ECC
{
public string title { get; set; }
public string url { get; set; }
}
public class ECB
{
public string title { get; set; }
public string url { get; set; }
}
然后你可以这样反序列化它:
Resources = JsonConvert.DeserializeObject<Rootobject>(resources.json)
下次小记:
如果您不确定 class 结构的外观(关于 json 字符串),您可以复制 json 字符串并转到:
Visual Studio -> Edit -> Paste Special -> Paste JSON as Classes
和Visual Studio将构成适当的class结构。
你的JSON不代表一个字典数组(Dictionary<string, Resource>[]
),它代表一个数组字典(Dictionary<string, Resource[]>
)。这就是您收到错误的原因。
此外,您的资源 class 需要像这样定义以匹配 JSON:
public class Resource
{
public string Title { get; set; }
public string Url { get; set; }
}
然后你可以这样反序列化:
Resources = JsonConvert.DeserializeObject<Dictionary<string, Resource[]>>(resources.json)
请在此处查看 fiddle:https://dotnetfiddle.net/8s2eQd