[英]How to get particular value from .json file
我有一個JSON文件,我需要捕獲一個特定的鍵值才能在這里執行我的JSON代碼
{
"uri": "https://wmg.nfa.futures.org/news/EnforceRegActions5imple.aspx",
"children": [
{
"uri": "https://www.nfa.futures.ortnews/EnforceRegActions5imple.aspx#145View%209%28e1233a13%401",
"children": [
{
"uri": "httpsghomm nfa futures org/news/EnforceReeActions5imple aspx#114Rule4%28e6ea03f2%400%40145View%209%28e1233a13%401",
"title": "Compliance Rules : Rule 2-2 : Rule",
"type": "Enforcement and Registration Actions : Rule",
"publishedDate": "2019-08-15",
"cachedDateTimeOffset": "0001-01-01700:00:00+00:00",
},
{
"uri": "httos./Pv'm nfa futures ore/news/EnforceReioctions5imple
aspx#114Rule4%28e6ea03f2%401%40145view%209%28e1233a13%401",
"title": "Compliance Rules : Rule 2-4 : Rule",
"type": "Enforcement and Registration Actions : Rule",
"publishedDate": "2019-08-15",
"cachedDateTimeOffset": "0001-01-01T00:00:00+00:00",
}
]
"children": [
{
"uri": "https://www.nfa.futures.ortnews/EnforceRegActions5imple.aspx#145View%209%28e1233a13%401",
"children": [
{
"uri": "httpsghomm nfa futures org/news/EnforceReeActions5imple aspx#114Rule4%28e6ea03f2%400%40145View%209%28e1233a13%401",
"title": "Compliance Rules : Rule 2-2 : Rule",
"type": "Enforcement and Registration Actions : Rule",
"publishedDate": "2019-08-15",
"cachedDateTimeOffset": "0001-01-01700:00:00+00:00",
"html": "<span data-rule-id=\"RULE 2-2\" data-rule-section=\"4\" data-rule-sub-section=\"0\" class.\"btn-add-rule\" data-cube-level=\"2\" data-cube-scope=\"7404961\">Rule</span>"
},
{
"uri": "httos./Pv'm nfa futures ore/news/EnforceReioctions5imple aspx#114Rule4%28e6ea03f2%401%40145view%209%28e1233a13%401",
"title": "Compliance Rules : Rule 2-4 : Rule",
"type": "Enforcement and Registration Actions : Rule",
"publishedDate": "2019-08-15",
"cachedDateTimeOffset": "0001-01-01T00:00:00+00:00",
"html": "<span data-rule-id=\"RuLE 2-4\" data-rule-section=\"4\" data-rule-sub-section=\"0\" class=\"btn-add-rule\" data-cube-level=\"2\" data-cube-scope=\"64787825\">Rule</span>"
}
]
}
]
}
如您在圖像中看到的,我需要Html部分來捕捉使用C#代碼,我能夠以對象的JSON形式獲取此代碼,但是我無法將此Html值選擇為變量
using (StreamReader r = new StreamReader("US--NFA--INS--ENFORCE.structure.json"))
{
string json = r.ReadToEnd();
var parsed = JObject.Parse(json);
當我調試時,我可以看到所有的值,但是我不能選擇特定的值,所以我也嘗試了SElECTED令牌,但是沒有運氣。
我的問題是,這有兩個孩子json一個有HTML密鑰,我不需要第一個孩子,我需要第二個孩子,我需要第二個孩子那個我需要的HTML值,但是當我嵌套循環時,它總是選擇第一個節點來解決這個問題
我看不到圖片,但您可以通過以下方式獲得:
parsed["key"];
下面的內容應該可以解決問題。
JArray data = (JArray)parsed["children"];
foreach(JObject children in (JObject)data){
foreach(JObject child in (JArray)children["children"]){
var value = child["html"];
}
}
也許這個例子會有所幫助:
string json = @"{
'channel': {
'title': 'James Newton-King',
'link': 'http://james.newtonking.com',
'description': 'James Newton-King\'s blog.',
'item': [
{
'title': 'Json.NET 1.3 + New license + Now on CodePlex',
'description': 'Announcing the release of Json.NET 1.3, the MIT license and the source on CodePlex',
'link': 'http://james.newtonking.com/projects/json-net.aspx',
'categories': [
'Json.NET',
'CodePlex'
]
},
{
'title': 'LINQ to JSON beta',
'description': 'Announcing LINQ to JSON',
'link': 'http://james.newtonking.com/projects/json-net.aspx',
'categories': [
'Json.NET',
'LINQ'
]
}
]
}
}";
JObject rss = JObject.Parse(json);
string rssTitle = (string)rss["channel"]["title"];
string itemTitle = (string)rss["channel"]["item"][0]["title"];
JArray categories = (JArray)rss["channel"]["item"][0]["categories"];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.