0

反序列化时,我在 json Web 服务中得到了字节数组的空值。但是从浏览器调用时它返回字节数组。C#代码:

var url = re.DownloadString("XXXX/ListService.svc/lstFooddtl/1/21");

 var a = JsonConvert.DeserializeObject<FooddtlResult>(url);

在 ai 中 food_photo 的值为空...当我反序列化... c# 类:

public class photo
    {
        public byte[] food_photo { get; set; }

    }
public class Food
{
    public int food_id { get; set; }
    public string food_name { get; set; }
    public photo[] food_photo { get; set; }
    public string unitcost { get; set; }
}
public class FooddtlResult
{
    public Food[] Result { get; set; }
}
{"Result":[{"food_id":"61","food_name":"Idli","food_photo":[255,216,255,224,0,....217],"unitcost":null}]}
4

1 回答 1

0

您的模型与 JSON 不匹配。为了正确的反序列化,将您的Food课程更改为

public class Food
{
    public int food_id { get; set; }
    public string food_name { get; set; }
    public byte[] food_photo { get; set; }
    public string unitcost { get; set; }
}

并删除photo课程。

否则,如果要保留模型结构,那么正确的 JSON 应该是:

{"Result":[{"food_id":"61","food_name":"Idli","food_photo":[{"food_photo":[255,216,255,224,0,....217]},{"food_photo":[255,...]},...],"unitcost":null}]}
于 2012-11-08T13:35:28.283 回答