c# - 序列化为 XML 时如何处理 JSON 键中的空格?

标签 c# .net xml json json.net

我在 .NET 4.0 应用程序中使用 Json.NET,以便将 JSON RESTful 响应转换为 XML。如果 JSON 子键有空格,我在将 JSON 转换为 XML 时遇到问题。

到目前为止,我能够转换大多数 JSON 响应。

Here are example responses along with the code which I am using to generate the XML.

{
   num_reviews: "2",
   page_id: "17816",
   merchant_id: 7165
}

这是导致错误的响应:

    [
    {
    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 编码?

最佳答案

此问题与 how to validate JSON string before converting to XML in C# 相同

如果您还有任何疑问,请告诉我。

关于c# - 序列化为 XML 时如何处理 JSON 键中的空格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17813635/

相关文章:

c# - 如何更改 DevExpress GridView 中一行的背景颜色?

c# - 带有源代码的 UWP 社区工具包

c# - 动态使用运行时编译的程序集

xml - 如何获取 XML 根元素及其属性,但没有内容

xml - xml 文件中的 Powershell 变量范围

c# - 在 C# 中解析非标准日期字符串

c# - Entity Framework Code First 中的多对多关系和使用 "virtual"关键字相互访问

.net - 在 C++/Cli 中使用 `__await` 等待 TPL 任务

c# - 如何拦截/覆盖 .net 可执行文件中的函数调用?

c++ - libxml2 是否维护文档顺序?