如何在 C# 中将非齐次 json 转换为字典 <string,string>?
How to convert non-homogeneous json to dictionary<string,string> in C#?
我正在做一个项目,我想将 json 数据转换为字典,但问题是 json 是非同质的。我正在使用 newtonsoft.json
这里是 JSON:
[{
'_type': 'StatsItem',
'Noofpostcomments': 0,
'URL': 'https: //www.website.com/',
'posts': 1,
'Brand': 'Halfords',
'Likes': 24884,
'movedTo': 'FB',
'sharing': 0,
'talked': 94,
'following': '24757',
'Date': '27-Mar-2018',
'unpopular': 0,
'ID': '2453'
},
{
'_type': 'StatsItem',
'qualify': 53,
'URL': 'https: //www.website.com/',
'post': 3,
'Brand': 'Kwik',
'Likes': 32339,
'movedTo': 'FB',
'share': 128,
'talk': 542,
'followers': '31824',
'Date': '27-Mar-2018',
'bands': 240,
'ID': '2453'
},
{
'_type': 'StatsItem',
'luck': 0,
'URL': 'https: //www.website.com/',
'bites': 0,
'Brand': 'SEAT',
'likes': 5329114,
'movedTo': 'FB',
'shares': 0,
'talking': 552,
'Followers': '5300000',
'Date': '27-Mar-2018',
'bands': 0,
'ID': '2453'
},
{
'_type': 'StatsItem',
'spoofs': 1,
'URL': 'https: //www.website.com/',
'security': 1,
'Brand': 'Honda',
'fixes': 184065,
'movedTo': 'FB',
'shares': 15,
'chat': 4251,
'follow': null,
'Date': '27-Mar-2018',
'discussed': 102,
'ID': '2453'
}]
代码如下:
Dictionary<string, string> columnDictionary = JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonString);
也试过
JObject result = JObject.Parse(jsonString);
出现异常
An unhandled exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll
Additional information: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'System.Collections.Generic.Dictionary`2[System.String,System.String]' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
Path '', line 1, position 1.
我哪里出错了,解决方法是什么?
您可以将其解析为字典数组(或列表):List<Dictionary<string, object>>
var list = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(jsonData);
foreach(var item in list)
{
foreach(KeyValuePair<string, object> keyVal in item)
{
Console.WriteLine($"{keyVal.Key}: {keyVal.Value?.ToString()}");
}
}
string responseString = @"[
json 描述了一些东西的数组,而不是一些东西的字典。首先尝试反序列化为对象数组,然后检查您在调试器中得到的内容,看看是否可以将其细化为更具体的类型对象。
此外,这是异端,但您也可以使用 System.Web.Extensions 中的 System.Web.Script.Serialization.JavaScriptSerializer,它更老更笨,可以帮助反序列化 'unknown typed json'
我正在做一个项目,我想将 json 数据转换为字典,但问题是 json 是非同质的。我正在使用 newtonsoft.json
这里是 JSON:
[{
'_type': 'StatsItem',
'Noofpostcomments': 0,
'URL': 'https: //www.website.com/',
'posts': 1,
'Brand': 'Halfords',
'Likes': 24884,
'movedTo': 'FB',
'sharing': 0,
'talked': 94,
'following': '24757',
'Date': '27-Mar-2018',
'unpopular': 0,
'ID': '2453'
},
{
'_type': 'StatsItem',
'qualify': 53,
'URL': 'https: //www.website.com/',
'post': 3,
'Brand': 'Kwik',
'Likes': 32339,
'movedTo': 'FB',
'share': 128,
'talk': 542,
'followers': '31824',
'Date': '27-Mar-2018',
'bands': 240,
'ID': '2453'
},
{
'_type': 'StatsItem',
'luck': 0,
'URL': 'https: //www.website.com/',
'bites': 0,
'Brand': 'SEAT',
'likes': 5329114,
'movedTo': 'FB',
'shares': 0,
'talking': 552,
'Followers': '5300000',
'Date': '27-Mar-2018',
'bands': 0,
'ID': '2453'
},
{
'_type': 'StatsItem',
'spoofs': 1,
'URL': 'https: //www.website.com/',
'security': 1,
'Brand': 'Honda',
'fixes': 184065,
'movedTo': 'FB',
'shares': 15,
'chat': 4251,
'follow': null,
'Date': '27-Mar-2018',
'discussed': 102,
'ID': '2453'
}]
代码如下:
Dictionary<string, string> columnDictionary = JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonString);
也试过
JObject result = JObject.Parse(jsonString);
出现异常
An unhandled exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll
Additional information: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'System.Collections.Generic.Dictionary`2[System.String,System.String]' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
Path '', line 1, position 1.
我哪里出错了,解决方法是什么?
您可以将其解析为字典数组(或列表):List<Dictionary<string, object>>
var list = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(jsonData);
foreach(var item in list)
{
foreach(KeyValuePair<string, object> keyVal in item)
{
Console.WriteLine($"{keyVal.Key}: {keyVal.Value?.ToString()}");
}
}
string responseString = @"[
json 描述了一些东西的数组,而不是一些东西的字典。首先尝试反序列化为对象数组,然后检查您在调试器中得到的内容,看看是否可以将其细化为更具体的类型对象。
此外,这是异端,但您也可以使用 System.Web.Extensions 中的 System.Web.Script.Serialization.JavaScriptSerializer,它更老更笨,可以帮助反序列化 'unknown typed json'