访问 Json 会获取特定的对象结果



我想从结果中获取 JSON 对象 uid 和分数以进行验证,但是当我尝试转换为字符串但结果始终为空时。

我的 Json:

[
{
"result":[
{
"uid":"1000",
"scores":[94.00659834]
}
]
}
]

我的代码: 临时列表是存储 JSON 数据的位置。

List<FaceIdentifyModel> tempList = new List<FaceIdentifyModel>();
// insert data into tempList
tb_Identify.Text = tempList.ToJson();
foreach (var t in tempList)
{
JObject jo_result = 
(JObject)JsonConvert.DeserializeObject(t.result.ToString());
JArray jo_age = 
(JArray)JsonConvert.DeserializeObject(jo_result["result"].ToString());
id = long.Parse(((JObject)t)["uid"].ToString());  //get uid
string scores = ((JObject)t)["scores"].ToString();//get scores
int num1 = scores.IndexOf("n") + 2;
int num2 = scores.LastIndexOf("]")-8;
ids = scores.Substring(num1, num2);
scores_num =double.Parse(ids);
} 

这是来自API

public static FaceIdentifyModel FaceIdentify(Image tempImage, string 
groupId, int userTopNum = 1, int faceTopNum = 1)
{
var client = new Face.Face(Config.clientId, Config.clientSecret);
var image1 = ImageHelper.ImageToBytes(tempImage, 
System.Drawing.Imaging.ImageFormat.Png);
var result = client.User.Identify(image1, new[] { groupId }, 
userTopNum, faceTopNum);
return result.ToObject<FaceIdentifyModel>();
}

这是 FaceIdentifyModel 类

{
public List<FaceIdentifyUserInfo> result { get; set; }
}
public class FaceIdentifyUserInfo
{
public string uid { get; set; }
public double[] scores { get; set; }
}

jo_result "空对象引用">

可以使用反序列化的 C# 对象:

List<FaceIdentifyModel> tempList = new List<FaceIdentifyModel>();
// insert data into tempList
tb_Identify.Text = tempList.ToJson();
foreach (var t in tempList)
{
id = long.Parse(t.uid);
scores_num = t.scores.First(); // or you might want to do average or sum
}

最新更新