C# 序列化为XML时如何处理JSON键中的空格?
我在.NET4.0应用程序中使用Json.NET,以便将Json RESTful响应转换为XML。如果JSON子键有空格,我会遇到将JSON转换为XML的问题 到目前为止,我能够转换大多数JSON响应 下面是导致错误的响应:C# 序列化为XML时如何处理JSON键中的空格?,c#,.net,xml,json,json.net,C#,.net,Xml,Json,Json.net,我在.NET4.0应用程序中使用Json.NET,以便将Json RESTful响应转换为XML。如果JSON子键有空格,我会遇到将JSON转换为XML的问题 到目前为止,我能够转换大多数JSON响应 下面是导致错误的响应: [ { headline: "ant bully", created_date: "2010/06/12", merchant_group_id: 10126, profile_id: 0, provider_id:
[
{
headline: "ant bully",
created_date: "2010/06/12",
merchant_group_id: 10126,
profile_id: 0,
provider_id: 10000,
locale: "en_US",
helpful_score: 1314,
locale_id: 1,
variant: "",
bottomline: "Yes",
name: "Jessie",
page_id: "17816",
review_tags: [
{
Pros: [
"Easy to Learn",
"Engaging Story Line",
"Graphics",
"Good Audio",
"Multiplayer",
"Gameplay"
]
},
{
Describe Yourself: [
"Casual Gamer"
]
},
{
Best Uses: [
"Multiple Players"
]
},
{
Primary use: [
"Personal"
]
}
],
rating: 4,
merchant_id: 7165,
reviewer_type: "Verified Reviewer",
comments: "fun to play"
},
{
headline: "Ok game, but great price!",
created_date: "2010/02/28",
merchant_group_id: 10126,
profile_id: 0,
provider_id: 10000,
locale: "en_US",
helpful_score: 1918,
locale_id: 1,
variant: "",
bottomline: "Yes",
name: "Alleycatsandconmen",
page_id: "17816",
review_tags: [
{
Pros: [
"Easy to Learn",
"Engaging Story Line"
]
},
{
Describe Yourself: [
"Frequent Player"
]
},
{
Primary use: [
"Personal"
]
},
{
Best Uses: [
"Kids"
]
}
],
rating: 3,
merchant_id: 7165,
reviewer_type: "Verified Reviewer",
comments: "This is a cute game for the kids and at a great price. Just don't expect a whole lot."
}
]
到目前为止,我一直在考虑创建JSON数据到C#对象的映射,并为该类生成XML。然而,有没有办法保持这种活力?或者有没有一种方法可以将空格视为%20编码?您可以调用它,它将使用\ucode>对任何无效字符进行转义
例如,一个空格将变成\ux0020\uu
您不能有一个包含空格的XMLElement名称。您需要用下划线或任何其他元素替换空格。如果这对您不可行,请尝试将该值作为该节点的属性。
我希望这是有意义的。这个问题与
如果您还有任何疑问,请告诉我。虽然这很好,但我在获取JsonConvert.DeserializeNode()以生成正确的XML时遇到了问题。原因是原始源代码没有尝试处理空格,这不是一个有效的JSON字符串(甚至不是javascript)。如果属性名称需要空格,则必须将其引用。要么修复响应,使其返回有效字符串,要么编写自己的解析器对其进行解析。
[
{
headline: "ant bully",
created_date: "2010/06/12",
merchant_group_id: 10126,
profile_id: 0,
provider_id: 10000,
locale: "en_US",
helpful_score: 1314,
locale_id: 1,
variant: "",
bottomline: "Yes",
name: "Jessie",
page_id: "17816",
review_tags: [
{
Pros: [
"Easy to Learn",
"Engaging Story Line",
"Graphics",
"Good Audio",
"Multiplayer",
"Gameplay"
]
},
{
Describe Yourself: [
"Casual Gamer"
]
},
{
Best Uses: [
"Multiple Players"
]
},
{
Primary use: [
"Personal"
]
}
],
rating: 4,
merchant_id: 7165,
reviewer_type: "Verified Reviewer",
comments: "fun to play"
},
{
headline: "Ok game, but great price!",
created_date: "2010/02/28",
merchant_group_id: 10126,
profile_id: 0,
provider_id: 10000,
locale: "en_US",
helpful_score: 1918,
locale_id: 1,
variant: "",
bottomline: "Yes",
name: "Alleycatsandconmen",
page_id: "17816",
review_tags: [
{
Pros: [
"Easy to Learn",
"Engaging Story Line"
]
},
{
Describe Yourself: [
"Frequent Player"
]
},
{
Primary use: [
"Personal"
]
},
{
Best Uses: [
"Kids"
]
}
],
rating: 3,
merchant_id: 7165,
reviewer_type: "Verified Reviewer",
comments: "This is a cute game for the kids and at a great price. Just don't expect a whole lot."
}
]