C# JsonArray 到字符串(不是字符串数组。仅字符串)
C# JsonArray to string (not string array. string only)
我一直在寻找这个,但找不到任何东西。为标题道歉,因为有很多关于将内容转换为字符串数组的内容,这不是我需要的。
我需要一种方法将 JsonArray 的内容按原样转换为字符串。 (类似于 JToken 的 .ToString())。我的场景具有这样的性质,即我需要一个数组的字符串,而不考虑其中包含的 type/s 。有一些方法可以使用 ValueConverter 处理弱类型 json,但我特别不想使用它们,因为我需要将字段的内容作为字符串传递给 WebView 中的 Javascript 函数。
我有以下 json(注意指示字符串在哪里的标记):
"highlights2":[
{
"_id":"highlight2-2850cb68121f9d4093e67950665c45fab02cec81",
"_rev":"9-c4345794001495104f8cbf5dd6999f3a",
"content":{ <---- Need this as string
"#roepman.17.0":[
[
233,
249,
"itsi-hl-gr"
],
[
298,
317,
"itsi-hl-bl"
]
],
"#roepman.19.0":[
[
5,
7,
"itsi-hl-gr"
]
]
}, <----- Up to here
"created":1434552587
}, //...more like this
],
"book":"book-930d62a2-9b7c-46a9-b092-f90469206900",
"serverTime":1435151280
理想情况下,我想将其解析为以下类型的列表:
public class HighlightInfo2
{
public string _id { get; set; }
public string _rev { get; set; }}
public string content { get; set; }
public long created { get; set; }
}
然而这是不可能的,因为 "content" 的内容是 JsonArray 类型。所以为了避免为 "content" 指定类型,我使用了这个:
public class HighlightInfo2
{
public string _id { get; set; }
public string _rev { get; set; }}
public Dictionary<string, List<JsonArray>> content { get; set; }
public long created { get; set; }
}
但这意味着我仍然必须在某些时候将字典中的 List< JsonArray > 转换为字符串,因为我稍后将 "content" 的内容传递给 webview 中的 Javascript 函数上。
有什么方法可以将 JsonArray 转换为字符串?
我的建议是在 highlightInfo2 class 下制作另一个 属性 类似 contentJson 并将 jsonarray 的字符串放入其中。
public class HighlightInfo2
{
private Dictionary<string, List<JsonArray>> _content;
public string _id { get; set; }
public string _rev { get; set; }
public Dictionary<string, List<JsonArray>> content
{
get { return _content; }
set
{
_content = value;
foreach (var item in _content)
{
contentJson += string.Join("\r\n", item.Value);
}
}
}
[JsonIgnore] //note, this depends on your json serializer
public string contentJson { set; get; }
public long created { get; set; }
}
根据您的评论,该字符串的格式无关紧要。它必须是有效的 JSON 代表原始数据。
然后我的建议是让 content
成员成为 object
类型的 HighlightInfo2
并简单地执行 JsonConvert.SerializeObject(highlightInfo.content)
以获得 JSON细绳。这就是您可以传递给 JavaScript 函数的内容。
如果您需要经常这样做,您可以将它与 结合起来,并在您的 class 中添加另一个成员来存储这个转换后的值。
结合使用 and ,我想出了这个 class/type 用于反序列化,这很顺利。感谢您的帮助。
public class HighlightInfo2
{
public string _id { get; set; }
public string _rev { get; set; }
public long created { get; set; }
private JToken _content { get; set; }
public JToken content
{
get { return _content; }
set
{
_content = value;
contentString = JsonConvert.SerializeObject(value);
}
}
[JsonIgnore]
public string contentString { get; set; }
}
我一直在寻找这个,但找不到任何东西。为标题道歉,因为有很多关于将内容转换为字符串数组的内容,这不是我需要的。
我需要一种方法将 JsonArray 的内容按原样转换为字符串。 (类似于 JToken 的 .ToString())。我的场景具有这样的性质,即我需要一个数组的字符串,而不考虑其中包含的 type/s 。有一些方法可以使用 ValueConverter 处理弱类型 json,但我特别不想使用它们,因为我需要将字段的内容作为字符串传递给 WebView 中的 Javascript 函数。
我有以下 json(注意指示字符串在哪里的标记):
"highlights2":[
{
"_id":"highlight2-2850cb68121f9d4093e67950665c45fab02cec81",
"_rev":"9-c4345794001495104f8cbf5dd6999f3a",
"content":{ <---- Need this as string
"#roepman.17.0":[
[
233,
249,
"itsi-hl-gr"
],
[
298,
317,
"itsi-hl-bl"
]
],
"#roepman.19.0":[
[
5,
7,
"itsi-hl-gr"
]
]
}, <----- Up to here
"created":1434552587
}, //...more like this
],
"book":"book-930d62a2-9b7c-46a9-b092-f90469206900",
"serverTime":1435151280
理想情况下,我想将其解析为以下类型的列表:
public class HighlightInfo2
{
public string _id { get; set; }
public string _rev { get; set; }}
public string content { get; set; }
public long created { get; set; }
}
然而这是不可能的,因为 "content" 的内容是 JsonArray 类型。所以为了避免为 "content" 指定类型,我使用了这个:
public class HighlightInfo2
{
public string _id { get; set; }
public string _rev { get; set; }}
public Dictionary<string, List<JsonArray>> content { get; set; }
public long created { get; set; }
}
但这意味着我仍然必须在某些时候将字典中的 List< JsonArray > 转换为字符串,因为我稍后将 "content" 的内容传递给 webview 中的 Javascript 函数上。
有什么方法可以将 JsonArray 转换为字符串?
我的建议是在 highlightInfo2 class 下制作另一个 属性 类似 contentJson 并将 jsonarray 的字符串放入其中。
public class HighlightInfo2
{
private Dictionary<string, List<JsonArray>> _content;
public string _id { get; set; }
public string _rev { get; set; }
public Dictionary<string, List<JsonArray>> content
{
get { return _content; }
set
{
_content = value;
foreach (var item in _content)
{
contentJson += string.Join("\r\n", item.Value);
}
}
}
[JsonIgnore] //note, this depends on your json serializer
public string contentJson { set; get; }
public long created { get; set; }
}
根据您的评论,该字符串的格式无关紧要。它必须是有效的 JSON 代表原始数据。
然后我的建议是让 content
成员成为 object
类型的 HighlightInfo2
并简单地执行 JsonConvert.SerializeObject(highlightInfo.content)
以获得 JSON细绳。这就是您可以传递给 JavaScript 函数的内容。
如果您需要经常这样做,您可以将它与
结合使用
public class HighlightInfo2
{
public string _id { get; set; }
public string _rev { get; set; }
public long created { get; set; }
private JToken _content { get; set; }
public JToken content
{
get { return _content; }
set
{
_content = value;
contentString = JsonConvert.SerializeObject(value);
}
}
[JsonIgnore]
public string contentString { get; set; }
}