我正在尝试获取JSON的内部节点。
尤其是我试图在" A"one_answers" B"中获得值。
但是,我的JSON以"事物"开头,然后转到" A"one_answers" B"。
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Rextester
{
public class Program
{
public static void Main(string[] args)
{
string json = "{"Things":[{"A":"one","B":"two","C":"three","D":"four","E":"five", "F":"six"},"G":"seven"}]}";
var jsonObj = JsonConvert.DeserializeObject<JObject>(json).First.First;
Console.WriteLine(jsonObj["Things"]);
}
public class Account
{
public string A
{
get;
set;
}
public string B
{
get;
set;
}
}
}
}
您的JSON假设有效JSON:
{
"Things": [{
"A": "one",
"B": "two",
"C": "three",
"D": "four",
"E": "five",
"F": "six" },
{"G": "seven"}]
}
您需要使用密钥,值对定义JSON的类,如下所示:
public class Myclass
{
public Dictionary<string, string>[] Things { get; set; }
}
那么您就很好:
string json = "{"Things":[{"A":"one","B":"two","C":"three","D":"four","E":"five", "F":"six"},{"G":"seven"}]}";
var jsonObj = JsonConvert.DeserializeObject<Myclass>(json);
Console.WriteLine(jsonObj.Things[0]["A"]);
Console.WriteLine(jsonObj.Things[0]["B"]);
输出:
one
two
为了帮助为JSON生成课程,您可以使用此网站http://json2csharp.com/当您拥有大JSON时,真的很有用。它还将验证您的JSON。