提问人:hud 提问时间:9/18/2023 最后编辑:Brian Tompsett - 汤莱恩hud 更新时间:9/19/2023 访问量:71
在 api 调用中完成反序列化对象后,在 JSON 字符串中找到的其他文本
Additional text found in JSON string after finishing deserializing object in api call
问:
我有一个 api,我想将其转换为 Datatable,因此我编写了以下代码。response
inputJsonIPColoMaster = (new JavaScriptSerializer()).Serialize(inputIPColoMaster);
WebClient client = new WebClient();
client.Headers["Content-type"] = "application/json";
client.Encoding = Encoding.UTF8;
json = client.UploadString(apiUrl, inputJsonIPColoMaster);
DataTable dtRes = new DataTable();
StringReader sr = new StringReader(json);
dtRes = JsonConvert.DeserializeObject<DataTable>(json); // here is the error
因此,在反序列化它时,我收到错误。
在 JSON 字符串中找到的其他文本...。
以下是我的回应。
{"Status":"Failed","Code":"400","Message":"Request initiation failed"}
答:
1赞
darrenleeyx
9/18/2023
#1
如果要在数据网格视图上显示响应,请按以下步骤实现:
(PS:我也看到了你的另一个问题 - 在数据表 c# 中分配 var 值)
var list = new List<ApiResponseModel>();
var json = "{\"Status\":\"Failed\",\"Code\":400,\"Message\":\"Request initiation failed\"}";
var model = JsonConvert.DeserializeObject<ApiResponseModel>(json);
model.Id = Guid.NewGuid(); //set NEID
list.Add(model);
dataGridView1.DataSource = list;
ApiResponseModel.cs
public class ApiResponseModel
{
public Guid Id { get; set; }
public string Status { get; set; }
public int Code { get; set; }
public string Message { get; set; }
}
评论
0赞
hud
9/19/2023
我会在某个时候检查并更新你..还有我应该怎么做才能带上NEID
0赞
darrenleeyx
9/19/2023
您能解释一下什么是NEID吗?
0赞
hud
9/19/2023
这是我需要带来的唯一值之一,以识别json值。
0赞
darrenleeyx
9/19/2023
您可以在模型中添加另一个属性,并在反序列化后分配该属性。请参阅更新后的答案:)
0赞
hud
9/19/2023
是的,它运行良好。还有一件事,我需要检查是否为空,并将数据插入到表中list
评论