【发布时间】:2016-12-15 22:06:23
【问题描述】:
我有一个返回 json 的 Json 对象。
我调用一个api并转换成json对象:
var returnJson = new JavaScriptSerializer().Deserialize(removeChar, targetType: null);
结果是:
{
"Type": "Local",
"results": {
"A": 4.4023,
"B": 1.6403,
"C": 2.3457
}
我如何读写这个 json 对象并只返回数组中的键?
我不确定 foreach 循环中发生了什么:
foreach(var item in returnJson)
{
//get just keys.ToArray();
}
类文件
public class BasicResults
{
public string Type { get; set; }
public Result results { get; set; }
}
public class Result
{
public double A { get; set; }
public double B { get; set; }
public double C { get; set; }
}
}
我认为这是正确的方向,但不能以嵌套速率为目标:
object[] getResults = returnJson.GetType()
.GetProperties()
.Select(p =>
{
object value = p.GetValue(results);
return value == null ? null : value.ToString();
})
.ToArray();
【问题讨论】:
-
您想获得示例中的 A、B、C 吗?
-
是的,就这些值。理想情况下以数组形式返回?
-
似乎与 4 小时前询问的 this question 非常相似。
-
removeChar 中有什么?我用你放的json字符串