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

时间:2018-06-10 08:33:50

标签: c# arrays json

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

我的杰森:

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

我的代码: tempList是存储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“空对象引用”

1 个答案:

答案 0 :(得分:0)

您可以使用反序列化的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
}