我有这样的json字符串输出:
string apiResult =
{
"error":[],
"result":{
"RARAGA":{
"a":["4","1","1.1"],
"b":["4","1","2"],
"c":["1","2"],
"v":["4","4"],
"p":["5","2"],
"t":[1],
"l":["3","4"],
"h":["5","7"],
"o":"8"
}
}
}
我要做的是用Newtonsoft.Json转换它:
dynamic result = JsonConvert.DeserializeObject(apiResult);
只将“RARAGA”属性的结果作为对象,所以我可以将其值取为a / b / c等。我只需要将其值视为:
result.result.RARAGA.a[0]
点是这个字符串“RARAGA”总是随机的
答案 0 :(得分:1)
如果您不知道属性的名称(或想要迭代所有属性),您可以使用JObject
JObject result = JsonConvert.DeserializeObject<JObject>(apiResult);
dynamic res = result["result"].First().First;
res将包含第一个对象的值作为动态变量
答案 1 :(得分:0)
如果您使用http://json2csharp.com/#,则下面应该是您应该将json字符串反序列化为
的模型public class RARAGA
{
public List<string> a { get; set; }
public List<string> b { get; set; }
public List<string> c { get; set; }
public List<string> v { get; set; }
public List<string> p { get; set; }
public List<int> t { get; set; }
public List<string> l { get; set; }
public List<string> h { get; set; }
public string o { get; set; }
}
public class Result
{
public RARAGA RARAGA { get; set; }
}
public class RootObject
{
public List<object> error { get; set; }
public Result result { get; set; }
}
所以你的反序列化将成为
RootObject result = JsonConvert.DeserializeObject<RootObject>(apiResult);
然后您可以像
一样访问它result.result.RARAGA.a[0]
答案 2 :(得分:0)
试试这个
dateTimeLabelFormats
使用结果[0] 代替 result.RARAGA
答案 3 :(得分:0)
使用Rahul的答案中的课程RARAGA
并转换JToken
(如ASpirin的答案):
public class RARAGA
{
public List<string> a { get; set; }
public List<string> b { get; set; }
public List<string> c { get; set; }
public List<string> v { get; set; }
public List<string> p { get; set; }
public List<int> t { get; set; }
public List<string> l { get; set; }
public List<string> h { get; set; }
public string o { get; set; }
}
JObject loObject = JObject.Parse(apiResult);
RARAGA loRARAGA = loObject["result"].First.First.ToObject<RARAGA>();
Console.WriteLine(loRARAGA.a[0]);