在 json 中删除 asp.net 中包含多值的属性 c# 在 gridview 中显示之前
remove attribute in json that contain multivalue in asp.net c# before show it in gridview
我是 asp.net c# 的新手。我想用 gridview 将 json 数据显示到 table 中。我有 json 这种格式的数据:
[{
"reviewerID": "A1YS9MDZP93857",
"asin": "0006428320",
"reviewerName": "John Taylor",
"helpful": [
0,
0
],
"reviewText": "last movement of sonata #6 is missing. What should one expect?",
"overall": 3,
"summary": "Parts missing",
"unixReviewTime": 1394496000,
"reviewTime": "03 11, 2014"
},
{
"reviewerID": "A3TS466QBAWB9D",
"asin": "0014072149",
"reviewerName": "Silver Pencil",
"helpful": [
0,
0
],
"reviewText": "If you are a serious violin student on a budget, this edition has it all",
"overall": 5,
"summary": "Perform it with a friend, today!",
"unixReviewTime": 1370476800,
"reviewTime": "06 6, 2013"
}]
为了将其显示到 gridview 中,我使用了以下代码(使用 Json.net 库)
JsonString = TextBox1.Text;
dt = (DataTable)JsonConvert.DeserializeObject(JsonString, (typeof(DataTable)));
GridView1.DataSource = dt;
GridView1.DataBind();
问题是 gridview 无法显示数据,如果我像这样一一删除 "helpfull" 属性就可以了:
{
"reviewerID": "A1YS9MDZP93857",
"asin": "0006428320",
"reviewerName": "John Taylor",
"reviewText": "The portfolio is fine except for the fact that the last movement of sonata #6 is missing. What should one expect?",
"overall": 3,
"summary": "Parts missing",
"unixReviewTime": 1394496000,
"reviewTime": "03 11, 2014"}
我不知道如何删除它的代码,因为我有大量 json 数据,手动删除它既困难又浪费时间。有什么想法吗?
当您使用 Json.NET 时,如果您想在反序列化为 DataTable
之前删除不需要的属性,您可以转换为中间 LINQ to JSON JToken
hierarchical representation, then remove properties with unwanted names. After this is done, you can deserialize to your final DataTable
class with JToken.ToObject<T>
.
要解析为 JToken
,请执行:
var obj = JToken.Parse(JsonString);
然后按名称删除不需要的属性:
var unwanteds = new HashSet<string>(new[]
{
"helpful"
});
foreach (var property in obj.Children().SelectMany(o => o.OfType<JProperty>()).Where(p => unwanteds.Contains(p.Name)).ToList())
{
property.Remove();
}
或者,要删除所有 除了 想要的属性,按名称:
var keepers = new HashSet<string>(new[]
{
"reviewerID",
"asin",
"reviewerName",
"reviewText",
"overall",
"summary",
"unixReviewTime",
"reviewTime",
});
foreach (var property in obj.Children().SelectMany(o => o.OfType<JProperty>()).Where(p => !keepers.Contains(p.Name)).ToList())
{
property.Remove();
}
最后,反序列化:
var dt = obj.ToObject<DataTable>();
我是 asp.net c# 的新手。我想用 gridview 将 json 数据显示到 table 中。我有 json 这种格式的数据:
[{
"reviewerID": "A1YS9MDZP93857",
"asin": "0006428320",
"reviewerName": "John Taylor",
"helpful": [
0,
0
],
"reviewText": "last movement of sonata #6 is missing. What should one expect?",
"overall": 3,
"summary": "Parts missing",
"unixReviewTime": 1394496000,
"reviewTime": "03 11, 2014"
},
{
"reviewerID": "A3TS466QBAWB9D",
"asin": "0014072149",
"reviewerName": "Silver Pencil",
"helpful": [
0,
0
],
"reviewText": "If you are a serious violin student on a budget, this edition has it all",
"overall": 5,
"summary": "Perform it with a friend, today!",
"unixReviewTime": 1370476800,
"reviewTime": "06 6, 2013"
}]
为了将其显示到 gridview 中,我使用了以下代码(使用 Json.net 库)
JsonString = TextBox1.Text;
dt = (DataTable)JsonConvert.DeserializeObject(JsonString, (typeof(DataTable)));
GridView1.DataSource = dt;
GridView1.DataBind();
问题是 gridview 无法显示数据,如果我像这样一一删除 "helpfull" 属性就可以了:
{
"reviewerID": "A1YS9MDZP93857",
"asin": "0006428320",
"reviewerName": "John Taylor",
"reviewText": "The portfolio is fine except for the fact that the last movement of sonata #6 is missing. What should one expect?",
"overall": 3,
"summary": "Parts missing",
"unixReviewTime": 1394496000,
"reviewTime": "03 11, 2014"}
我不知道如何删除它的代码,因为我有大量 json 数据,手动删除它既困难又浪费时间。有什么想法吗?
当您使用 Json.NET 时,如果您想在反序列化为 DataTable
之前删除不需要的属性,您可以转换为中间 LINQ to JSON JToken
hierarchical representation, then remove properties with unwanted names. After this is done, you can deserialize to your final DataTable
class with JToken.ToObject<T>
.
要解析为 JToken
,请执行:
var obj = JToken.Parse(JsonString);
然后按名称删除不需要的属性:
var unwanteds = new HashSet<string>(new[]
{
"helpful"
});
foreach (var property in obj.Children().SelectMany(o => o.OfType<JProperty>()).Where(p => unwanteds.Contains(p.Name)).ToList())
{
property.Remove();
}
或者,要删除所有 除了 想要的属性,按名称:
var keepers = new HashSet<string>(new[]
{
"reviewerID",
"asin",
"reviewerName",
"reviewText",
"overall",
"summary",
"unixReviewTime",
"reviewTime",
});
foreach (var property in obj.Children().SelectMany(o => o.OfType<JProperty>()).Where(p => !keepers.Contains(p.Name)).ToList())
{
property.Remove();
}
最后,反序列化:
var dt = obj.ToObject<DataTable>();