ASP.NET C# Web API: JsonConvert.SerializeObject 奇怪 'Escapes' 数组
ASP.NET C# Web API: JsonConvert.SerializeObject Weirdly 'Escapes' Array
我正在尝试构建一个简单的 GET API,它将从 API 获取数据并使我能够从我的前端 (Javascript) 获取它,但是我遇到的问题我面临的是当返回 JsonConvert.SerializeObject
它奇怪地转义了一个存储在数据库中的数组并且正在成为前端解析的噩梦:
代码:
public string Get()
{
sqlQuery =
"SELECT TOP 60 * FROM tb_HandoverDetails ORDER BY SubmittedDateTimeUTC DESC";
SqlDataAdapter da = new SqlDataAdapter(sqlQuery, conn);
DataTable dt = new DataTable();
da.Fill (dt);
if (dt.Rows.Count > 0)
{
return JsonConvert.SerializeObject(dt);
}
else
{
Response.StatusCode = 400;
return "no data found";
}
}
结果:
[
{
"ID": 8,
"Submitter": "auth.user",
"SubmittedDateTimeUTC": "2021-10-05T20:29:13",
"ExcelTableOne": "\"[{\\"caseID\\":[\\"123\\",\\"1234\\",\\"12345\\",\\"123456\\",\\"1234567\\",\\"12345678\\",\\"123456789\\"]},{\\"owner\\":[]},{\\"assignee\\":[]},{\\"comments\\":[]}]\""
},
]
ExcelTableOne
是奇怪转义的数组
数据库中的 ExcelTableOne 数据:
"[{\"caseID\":[\"123\",\"1234\",\"12345\",\"123456\",\"1234567\",\"12345678\",\"123456789\"]},{\"owner\":[\"Ayush Lal\"]},{\"assignee\":[]},{\"comments\":[]}]"
有什么想法吗?
TIA
您再次序列化了已经序列化的ExcelTableOne
要修复,试试这个
var resultStr= Get();
var resultPrev= JsonConvert.DeserializeObject<List<SubmiterStr>>(resultStr);
var result = resultPrev.Select(p => new Submiter {Id=p.Id, Submitter=p.Submitter, SubmittedDateTimeUtc=p.SubmittedDateTimeUtc}).ToList();
for (int i = 0; i < result.Count; i++)
{
var excelTableStr = JsonConvert.DeserializeObject<string>(resultPrev[i].ExcelTableOneStr);
result[i].ExcelTableOne= JsonConvert.DeserializeObject<List<ExcelTableOne>>(excelTableStr);
}
类
public partial class SubmiterStr
{
[JsonProperty("ID")]
public long Id { get; set; }
[JsonProperty("Submitter")]
public string Submitter { get; set; }
[JsonProperty("SubmittedDateTimeUTC")]
public DateTimeOffset SubmittedDateTimeUtc { get; set; }
[JsonProperty("ExcelTableOne")]
public string ExcelTableOneStr { get; set; }
}
public partial class Submiter
{
[JsonProperty("ID")]
public long Id { get; set; }
[JsonProperty("Submitter")]
public string Submitter { get; set; }
[JsonProperty("SubmittedDateTimeUTC")]
public DateTimeOffset SubmittedDateTimeUtc { get; set; }
[JsonProperty("ExcelTableOne")]
public List<ExcelTableOne> ExcelTableOne { get; set; }
}
public partial class ExcelTableOne
{
[JsonProperty("caseID", NullValueHandling = NullValueHandling.Ignore)]
//[JsonConverter(typeof(DecodeArrayConverter))]
public long[] CaseId { get; set; }
[JsonProperty("owner", NullValueHandling = NullValueHandling.Ignore)]
public object[] Owner { get; set; }
[JsonProperty("assignee", NullValueHandling = NullValueHandling.Ignore)]
public object[] Assignee { get; set; }
[JsonProperty("comments", NullValueHandling = NullValueHandling.Ignore)]
public object[] Comments { get; set; }
}
结果
[
{
"ID": 8,
"Submitter": "auth.user",
"SubmittedDateTimeUTC": "2021-10-05T20:29:13-02:30",
"ExcelTableOne": [
{
"caseID": [
123,
134,
12345,
123456,
1234567,
12345678,
123456789
]
},
{
"owner": []
},
{
"assignee": []
},
{
"comments": []
}
]
}
]
我正在尝试构建一个简单的 GET API,它将从 API 获取数据并使我能够从我的前端 (Javascript) 获取它,但是我遇到的问题我面临的是当返回 JsonConvert.SerializeObject
它奇怪地转义了一个存储在数据库中的数组并且正在成为前端解析的噩梦:
代码:
public string Get()
{
sqlQuery =
"SELECT TOP 60 * FROM tb_HandoverDetails ORDER BY SubmittedDateTimeUTC DESC";
SqlDataAdapter da = new SqlDataAdapter(sqlQuery, conn);
DataTable dt = new DataTable();
da.Fill (dt);
if (dt.Rows.Count > 0)
{
return JsonConvert.SerializeObject(dt);
}
else
{
Response.StatusCode = 400;
return "no data found";
}
}
结果:
[
{
"ID": 8,
"Submitter": "auth.user",
"SubmittedDateTimeUTC": "2021-10-05T20:29:13",
"ExcelTableOne": "\"[{\\"caseID\\":[\\"123\\",\\"1234\\",\\"12345\\",\\"123456\\",\\"1234567\\",\\"12345678\\",\\"123456789\\"]},{\\"owner\\":[]},{\\"assignee\\":[]},{\\"comments\\":[]}]\""
},
]
ExcelTableOne
是奇怪转义的数组
数据库中的 ExcelTableOne 数据:
"[{\"caseID\":[\"123\",\"1234\",\"12345\",\"123456\",\"1234567\",\"12345678\",\"123456789\"]},{\"owner\":[\"Ayush Lal\"]},{\"assignee\":[]},{\"comments\":[]}]"
有什么想法吗?
TIA
您再次序列化了已经序列化的ExcelTableOne
要修复,试试这个
var resultStr= Get();
var resultPrev= JsonConvert.DeserializeObject<List<SubmiterStr>>(resultStr);
var result = resultPrev.Select(p => new Submiter {Id=p.Id, Submitter=p.Submitter, SubmittedDateTimeUtc=p.SubmittedDateTimeUtc}).ToList();
for (int i = 0; i < result.Count; i++)
{
var excelTableStr = JsonConvert.DeserializeObject<string>(resultPrev[i].ExcelTableOneStr);
result[i].ExcelTableOne= JsonConvert.DeserializeObject<List<ExcelTableOne>>(excelTableStr);
}
类
public partial class SubmiterStr
{
[JsonProperty("ID")]
public long Id { get; set; }
[JsonProperty("Submitter")]
public string Submitter { get; set; }
[JsonProperty("SubmittedDateTimeUTC")]
public DateTimeOffset SubmittedDateTimeUtc { get; set; }
[JsonProperty("ExcelTableOne")]
public string ExcelTableOneStr { get; set; }
}
public partial class Submiter
{
[JsonProperty("ID")]
public long Id { get; set; }
[JsonProperty("Submitter")]
public string Submitter { get; set; }
[JsonProperty("SubmittedDateTimeUTC")]
public DateTimeOffset SubmittedDateTimeUtc { get; set; }
[JsonProperty("ExcelTableOne")]
public List<ExcelTableOne> ExcelTableOne { get; set; }
}
public partial class ExcelTableOne
{
[JsonProperty("caseID", NullValueHandling = NullValueHandling.Ignore)]
//[JsonConverter(typeof(DecodeArrayConverter))]
public long[] CaseId { get; set; }
[JsonProperty("owner", NullValueHandling = NullValueHandling.Ignore)]
public object[] Owner { get; set; }
[JsonProperty("assignee", NullValueHandling = NullValueHandling.Ignore)]
public object[] Assignee { get; set; }
[JsonProperty("comments", NullValueHandling = NullValueHandling.Ignore)]
public object[] Comments { get; set; }
}
结果
[
{
"ID": 8,
"Submitter": "auth.user",
"SubmittedDateTimeUTC": "2021-10-05T20:29:13-02:30",
"ExcelTableOne": [
{
"caseID": [
123,
134,
12345,
123456,
1234567,
12345678,
123456789
]
},
{
"owner": []
},
{
"assignee": []
},
{
"comments": []
}
]
}
]