[英]How to turn plain text json data into string?
I am trying to mock up some test data to check wether a json string deserializes into an object correctly. 我试图模拟一些测试数据来检查json字符串是否正确地反序列化为对象。
I have some json data which is 660 lines long, so i have only included a portion 我有一些长度为660行的json数据,所以我只包含了一部分
{
"DataA": "string",
"DataB": "datetime",
"DataC": {
"DataC1": "datetime",
"DataC2": "datetime",
"DataC3": "datetime",
"DataC4": int,
"DataC5": int,
"DataC6": "string",
"DataC7": int,
"DataC8": "object"
},
"DataD": {
"DataD1": decimal,
"DataD2": decimal,
"DataD3": "string",
"DataD4": int,
"DataD5": decimal,
"DataD6": "string",
"DataD7": {
"DataD7i": null,
"DataD7ii": [
I have created the corresponding classes, but am currently attempting to test them. 我已经创建了相应的类,但我正在尝试测试它们。 However I am unable to get this json data into a string, as the double quotation marks close off the string. 但是我无法将此json数据转换为字符串,因为双引号关闭字符串。 I have tried using ecsapes aswell but to no avail. 我曾尝试过使用ecsapes但无济于事。
string testjson = "{
"DataA": "string",
"DataB": "datetime",
"DataC": {
"DataC1": "datetime",
"DataC2": "datetime",
"DataC3": "datetime",
"DataC4": int,
"DataC5": int,
"DataC6": "string",
"DataC7": int,
"DataC8": "object"
},
"DataD": {
"DataD1": decimal,
"DataD2": decimal,
"DataD3": "string",
"DataD4": int,
"DataD5": decimal,
"DataD6": "string",
"DataD7": {
"DataD7i": null,
"DataD7ii": ["
I want to call 我想打电话
ObjectA objectblah= JsonConvert.DeserializeObject<ObjectA>(output);
But cannot manage to get the json into a string. 但无法设法将json变成字符串。 I am aware this is a trivial issue, but I am new and am stuck on this issue. 我知道这是一个微不足道的问题,但我是新的,我坚持这个问题。 any help would be greatly appreciated. 任何帮助将不胜感激。
Thanks 谢谢
问题的一部分看起来是使用双引号,可以使用反斜杠\\
来转义,但是要在C#中使用多行字符串,您还需要在开头添加@
符号,如此答案https中所示: //stackoverflow.com/a/1100265/2603735
In my unit test projects, whenever I have "mass" text, I put that content into a separate text file. 在我的单元测试项目中,每当我有“大量”文本时,我都会将该内容放入单独的文本文件中。 Then you have two choices: 那你有两个选择:
Keeping it in a separate file makes editing/maintenance a lot easier. 将其保存在单独的文件中使编辑/维护变得更加容易。
Use it like this: 像这样使用它:
string testjson = @"
{
DataA: string,
DataB: datetime,
DataC: {
DataC1: datetime,
DataC2: datetime,
DataC3: datetime,
DataC4: int,
DataC5: int,
DataC6: string,
DataC7: int,
DataC8: object
},
DataD: {
DataD1: decimal,
DataD2: decimal,
DataD3: string,
DataD4: int,
DataD5: decimal,
DataD6: string,
DataD7: {
DataD7i: null,
DataD7ii: []
}
}
}"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.