Search code examples
json.netjsonconvert

Why JSON deserializer returns null value when JSON string is not empty?


I am trying to deserialize the following string :

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

I tested this string here and it's decoding is as I wanted. But however, C# function does not work as expected.

    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" result is null always.

Any help ?


Solution

  • You're going to want something in this format. Your names are off and the predictions with having [[]] are a list of lists.

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