0
我想从键值对中的节点的基础下从JSON提取值。我需要将JSON字段的值存储在列表或集合中。我尝试了下面的代码,但它没有给出所需的输出。我怎样才能做到这一点?如何将值从JSON存储到键值对
截至目前,我这样做:
HttpPostedFileBase JSfile = Request.Files["UploadedJSFile"];
if ((JSfile != null) && (JSfile.ContentLength > 0) && !string.IsNullOrEmpty(JSfile.FileName))
{
BinaryReader b = new BinaryReader(JSfile.InputStream);
byte[] binData = b.ReadBytes(JSfile.ContentLength);
string result = System.Text.Encoding.UTF8.GetString(binData);
JArray jsonVal = JArray.Parse(result) as JArray;
foreach (JObject content in jsonVal.Children<JObject>())
{
foreach (JProperty prop in content.Properties())
{
filters.Add(prop.Name, prop.Value.ToString());
}
}
}
但它不给所需的输出。
我想和喜欢他们的节点的所有值:herobanner.landing.copies.watchvideo
这里是我的JSON:
[{
"country": "en-in",
"heroBanner": {
"landing": {
"copies": {
"watchVideo": "watch the video",
"scrollDown": [
"READ INSPIRING STORIES",
""
],
"banner": [
"make your",
"first move this",
"valentine's Day"
]
},
"background": {
"desktop": "assets/images/millions-hero-1.jpg"
},
"foreground": {
"desktop": ""
},
"video": {
"youtubeId": "buwcDIcFR8I"
}
}
}
}]
如果您不关心强类型解决方案,请尝试使用'dynamic'。它将允许您访问您提供的任何财产。 –
可以给我的代码行,我cn使用 – John
'dynamic foo = JObject.Parse(jsonText); string bar = foo.Bar;'see [link](https://thewayofcode.wordpress.com/2012/09/18/c-dynamic-object-and-json-serialization-with-json-net/) –