gpt4 book ai didi

c# - json 接受单个值作为数组

转载 作者:行者123 更新时间:2023-11-30 20:58:20 25 4
gpt4 key购买 nike

json如何接受单个值作为数组?

这个 json 抛出异常:

{ "code": "1", "message": "OK", "response": { "partners": { "id": "33", "name": "", "clienttypeid": "29", "logo": "", "description": "", "website": "www.site.com" } } }

这个 json 解析正确:

{ "code": "1", "message": "OK", "response": { "partners": [ { "id": "33", "name": "", "clienttypeid": "29", "logo": "", "description": "", "website": "www.site.com" }, { "id": "34", "name": "", "clienttypeid": "29", "logo": "", "description": "", "website": "www.site.com" } ] } }

型号:

public class Partner
{
public string id { get; set; }
public string name { get; set; }
public string clienttypeid { get; set; }
public string logo { get; set; }
public string description { get; set; }
public string website { get; set; }
}

public class Response
{
public List<Partner> partners { get; set; }
}

public class RootObject
{
public string code { get; set; }
public string message { get; set; }
public Response response { get; set; }
}

最佳答案

如果您尝试反序列化为 List,请使用数组表示法,即使只有一个元素也是如此

{
"code":"1",
"message":"OK",
"response":{
"partners":[
{
"id":"33",
"name":"",
"clienttypeid":"29",
"logo":"",
"description":"",
"website":"www.site.com"
}
]
}
}

关于c# - json 接受单个值作为数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16192614/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com