【问题标题】:Why JSON deserializer returns null value when JSON string is not empty?为什么 JSON 字符串不为空时 JSON 反序列化器返回空值?
【发布时间】:2019-07-11 11:18:36
【问题描述】:

我正在尝试反序列化以下字符串:

{"image":"c:\testimage\test.jpg","predictions":[[0.0000103891,0.0128408,0.914102,0.0000968333,0.0729495]]}

我测试了这个字符串here,它的解码是我想要的。但是,C# 函数并没有按预期工作。

    public class ServerResponse
    {
        [DataMember]
        public string PredictImage { get; set; }
        [DataMember]
        public string[] JSONresult { get; set; }
    }

        private void button9_Click(object sender, EventArgs e)
        {
            string strResponse = txtJSONstring.Text;
            ServerResponse jsonResult = new ServerResponse();
            jsonResult = JsonConvert.DeserializeObject<ServerResponse>(strResponse);
            txtJSONresult.AppendText(jsonResult.PredictImage);
//            txtJSONresult.AppendText(jsonResult.JSONresult);
        }

“jsonResult”结果始终为空。

有什么帮助吗?

【问题讨论】:

  • 您的 ServerResponse 对象与 JSON 的属性名称不同。
  • 哦,谢谢,让我检查一下

标签: json.net jsonconvert


【解决方案1】:

你会想要这种格式的东西。您的名字已关闭,带有 [[]] 的预测是列表列表。

public class ServerResponse
{
    public string image { get; set; }
    public List<List<double>> predictions { get; set; }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-09
    • 1970-01-01
    • 2022-12-03
    • 1970-01-01
    相关资源
    最近更新 更多