使用 Newtonsoft 序列化 Json 个对象的列表会导致许多“\r\n”格式不正确
Serializing List of Json Objects with Newtonsoft results in incorrect formatting with many "\r\n"
下面是我的代码片段:
private void Save(object sender, EventArgs e)
{
var readText = File.ReadAllText(FilePath);
JObject jsonObject = JsonConvert.DeserializeObject(readText) as JObject;
string cfgStr = JsonConvert.SerializeObject(ListViewContents, Formatting.Indented);
jsonObject["JoystickName"]["input_bindings"] = cfgStr;
File.WriteAllText(FilePath, jsonObject.ToString());
Navigation.PushAsync(new MainPage());
}
我正在尝试序列化 json 个对象的列表,ListViewContents 是一个对象列表,因为每个输入绑定都可以不同。当我序列化列表并写入文件时,它会将所有输入绑定写入一行,并将其作为输出:
"input_bindings": "[\r\n {\r\n \"topic\": \"hwi.te0_ptt_wog.cableCutCover\",\r\n \"input_type\": \"button\",\r\n \"output_type\": \"boolean\",\r\n \"button_id\": \"DPad Down\",\r\n \"invert\": true\r\n },\r\n {\r\n \"topic\": \"hwi.te0_ptt_wog.cableCut\",\r\n \"input_type\": \"button\",\r\n \"output_type\": \"boolean\",\r\n \"button_id\": \"DPad Left\"\r\n },\r\n {\r\n \"topic\": \"hwi.te0_ptt_wog.winchArmSwitch\",\r\n \"input_type\": \"button\",\r\n \"output_type\": \"boolean\",\r\n \"button_id\": \"DPad Right\"\r\n },\r\n {\r\n \"topic\": \"hwi.te0_ptt_wog.bottomRight\",\r\n \"input_type\": \"button\",\r\n \"output_type\": \"boolean\",\r\n \"button_id\": \"L3\"\r\n },\r\n {\r\n \"topic\": \"hwi.te0_ptt_wog.topLeft\",\r\n \"input_type\": \"button\",\r\n \"output_type\": \"boolean\",\r\n \"button_id\": \"Select\"\r\n },\r\n {\r\n \"topic\": \"hwi.te0_ptt_wog.mic_en\",\r\n \"input_type\": \"button\",\r\n \"output_type\": \"boolean\",\r\n \"button_id\": \"Start\"\r\n },\r\n {\r\n \"topic\": \"hwi.te0_ptt_wog.hot_mic_en\",\r\n \"input_type\": \"button\",\r\n \"output_type\": \"boolean\",\r\n \"button_id\": \"R3\"\r\n },\r\n {\r\n \"topic\": \"hwi.te0_ptt_wog.wog_present\",\r\n \"input_type\": \"button\",\r\n \"output_type\": \"boolean\",\r\n \"button_id\": \"DPad Up\"\r\n },\r\n {\r\n \"topic\": \"hwi.te0_ptt_wog.winchCommand\",\r\n \"input_type\": \"axis\",\r\n \"output_type\": \"analog\",\r\n \"axis_id\": \"Trigger Left\",\r\n \"scalar\": -1.0,\r\n \"bias\": 0.0\r\n }\r\n]"
理想情况下,整个 JSON 文件如下所示:
{
"g" : "${import:../global.json}",
"JoystickName": {
"output_uri" : "placeholder",
"ctrl_uri" : "placeholder",
"log": {
"level": 2,
"uri": "placeholder"
},
"device": "placeholder",
"input_bindings": [
{
"topic": "hwi.te0_ptt_wog.cableCutCover",
"input_type": "button",
"output_type": "boolean",
"button_id": "DPad Down",
"invert" : true
},
{
"topic": "hwi.te0_ptt_wog.cableCut",
"input_type": "button",
"output_type": "boolean",
"button_id": "DPad Left"
},
{
"topic": "hwi.te0_ptt_wog.winchArmSwitch",
"input_type": "button",
"output_type": "boolean",
"button_id": "DPad Right"
},
{
"topic": "hwi.te0_ptt_wog.bottomRight",
"input_type": "button",
"output_type": "boolean",
"button_id": "L3"
},
{
"topic": "hwi.te0_ptt_wog.topLeft",
"input_type": "button",
"output_type": "boolean",
"button_id": "Select"
},
{
"topic": "hwi.te0_ptt_wog.mic_en",
"input_type": "button",
"output_type": "boolean",
"button_id": "Start"
},
{
"topic": "hwi.te0_ptt_wog.hot_mic_en",
"input_type": "button",
"output_type": "boolean",
"button_id": "R3"
},
{
"topic": "hwi.te0_ptt_wog.wog_present",
"input_type": "button",
"output_type": "boolean",
"button_id": "DPad Up"
},
{
"topic": "hwi.te0_ptt_wog.winchCommand",
"input_type": "axis",
"output_type": "analog",
"axis_id": "Trigger Left",
"scalar": -1.0,
"bias": 0.0
}
]
}
}
一旦我将“input_bindings”分配给 cfgStr,就会出现这种奇怪的格式。
任何帮助将不胜感激,我在搜索时找不到与我的问题相似的帖子。
这是问题所在:
string cfgStr = JsonConvert.SerializeObject(ListViewContents, Formatting.Indented);
jsonObject[JoystickName]["input_bindings"] = cfgStr;
您将 input_bindings
的值设置为“序列化 JSON 的结果”(即字符串)。然后,您将序列化整个对象(使用 jsonObject.ToString()
),以便序列化该字符串,包括转义所有内容。您只想序列化 一次.
我怀疑你只是想要:
jsonObject[JoystickName]["input_bindings"] = JArray.FromObject(ListViewContents);
换句话说,将 ListViewContents
转换为 LINQ to JSON 模型,但 还没有 将其序列化为字符串。
下面是我的代码片段:
private void Save(object sender, EventArgs e)
{
var readText = File.ReadAllText(FilePath);
JObject jsonObject = JsonConvert.DeserializeObject(readText) as JObject;
string cfgStr = JsonConvert.SerializeObject(ListViewContents, Formatting.Indented);
jsonObject["JoystickName"]["input_bindings"] = cfgStr;
File.WriteAllText(FilePath, jsonObject.ToString());
Navigation.PushAsync(new MainPage());
}
我正在尝试序列化 json 个对象的列表,ListViewContents 是一个对象列表,因为每个输入绑定都可以不同。当我序列化列表并写入文件时,它会将所有输入绑定写入一行,并将其作为输出:
"input_bindings": "[\r\n {\r\n \"topic\": \"hwi.te0_ptt_wog.cableCutCover\",\r\n \"input_type\": \"button\",\r\n \"output_type\": \"boolean\",\r\n \"button_id\": \"DPad Down\",\r\n \"invert\": true\r\n },\r\n {\r\n \"topic\": \"hwi.te0_ptt_wog.cableCut\",\r\n \"input_type\": \"button\",\r\n \"output_type\": \"boolean\",\r\n \"button_id\": \"DPad Left\"\r\n },\r\n {\r\n \"topic\": \"hwi.te0_ptt_wog.winchArmSwitch\",\r\n \"input_type\": \"button\",\r\n \"output_type\": \"boolean\",\r\n \"button_id\": \"DPad Right\"\r\n },\r\n {\r\n \"topic\": \"hwi.te0_ptt_wog.bottomRight\",\r\n \"input_type\": \"button\",\r\n \"output_type\": \"boolean\",\r\n \"button_id\": \"L3\"\r\n },\r\n {\r\n \"topic\": \"hwi.te0_ptt_wog.topLeft\",\r\n \"input_type\": \"button\",\r\n \"output_type\": \"boolean\",\r\n \"button_id\": \"Select\"\r\n },\r\n {\r\n \"topic\": \"hwi.te0_ptt_wog.mic_en\",\r\n \"input_type\": \"button\",\r\n \"output_type\": \"boolean\",\r\n \"button_id\": \"Start\"\r\n },\r\n {\r\n \"topic\": \"hwi.te0_ptt_wog.hot_mic_en\",\r\n \"input_type\": \"button\",\r\n \"output_type\": \"boolean\",\r\n \"button_id\": \"R3\"\r\n },\r\n {\r\n \"topic\": \"hwi.te0_ptt_wog.wog_present\",\r\n \"input_type\": \"button\",\r\n \"output_type\": \"boolean\",\r\n \"button_id\": \"DPad Up\"\r\n },\r\n {\r\n \"topic\": \"hwi.te0_ptt_wog.winchCommand\",\r\n \"input_type\": \"axis\",\r\n \"output_type\": \"analog\",\r\n \"axis_id\": \"Trigger Left\",\r\n \"scalar\": -1.0,\r\n \"bias\": 0.0\r\n }\r\n]"
理想情况下,整个 JSON 文件如下所示:
{
"g" : "${import:../global.json}",
"JoystickName": {
"output_uri" : "placeholder",
"ctrl_uri" : "placeholder",
"log": {
"level": 2,
"uri": "placeholder"
},
"device": "placeholder",
"input_bindings": [
{
"topic": "hwi.te0_ptt_wog.cableCutCover",
"input_type": "button",
"output_type": "boolean",
"button_id": "DPad Down",
"invert" : true
},
{
"topic": "hwi.te0_ptt_wog.cableCut",
"input_type": "button",
"output_type": "boolean",
"button_id": "DPad Left"
},
{
"topic": "hwi.te0_ptt_wog.winchArmSwitch",
"input_type": "button",
"output_type": "boolean",
"button_id": "DPad Right"
},
{
"topic": "hwi.te0_ptt_wog.bottomRight",
"input_type": "button",
"output_type": "boolean",
"button_id": "L3"
},
{
"topic": "hwi.te0_ptt_wog.topLeft",
"input_type": "button",
"output_type": "boolean",
"button_id": "Select"
},
{
"topic": "hwi.te0_ptt_wog.mic_en",
"input_type": "button",
"output_type": "boolean",
"button_id": "Start"
},
{
"topic": "hwi.te0_ptt_wog.hot_mic_en",
"input_type": "button",
"output_type": "boolean",
"button_id": "R3"
},
{
"topic": "hwi.te0_ptt_wog.wog_present",
"input_type": "button",
"output_type": "boolean",
"button_id": "DPad Up"
},
{
"topic": "hwi.te0_ptt_wog.winchCommand",
"input_type": "axis",
"output_type": "analog",
"axis_id": "Trigger Left",
"scalar": -1.0,
"bias": 0.0
}
]
}
}
一旦我将“input_bindings”分配给 cfgStr,就会出现这种奇怪的格式。
任何帮助将不胜感激,我在搜索时找不到与我的问题相似的帖子。
这是问题所在:
string cfgStr = JsonConvert.SerializeObject(ListViewContents, Formatting.Indented);
jsonObject[JoystickName]["input_bindings"] = cfgStr;
您将 input_bindings
的值设置为“序列化 JSON 的结果”(即字符串)。然后,您将序列化整个对象(使用 jsonObject.ToString()
),以便序列化该字符串,包括转义所有内容。您只想序列化 一次.
我怀疑你只是想要:
jsonObject[JoystickName]["input_bindings"] = JArray.FromObject(ListViewContents);
换句话说,将 ListViewContents
转换为 LINQ to JSON 模型,但 还没有 将其序列化为字符串。