XML 到 Json 没有按照我期望的方式处理数组

发布于 2024-12-29 18:35:29 字数 738 浏览 3 评论 0原文

我有一个 api,可以使用 XML 或 Json 格式进行响应。为了分析这些结果,我创建了一个要填充 json 和 xml 的对象。问题是我的 XML 解析器似乎以稍微不同的格式将 XML 反序列化为 Json。

我的回复中间是一个嵌套数组 当我解析 JSON 时,看起来像这样

    "buckets":[
{"bucket":{"name":"soup","priority":10}},
{"bucket":{"name":"clams","priority":13}}],

当我将 XML 解析为 JSON 时,它看起来像这样

"buckets":{"bucket":[
{"priority":"10","name":"soup"},
{"priority":"13","name":"clams"}]},

我为保存此日期而创建的单个对象有一个私有列表桶; json 解析处理得很好,而 xml 解析会抛出异常。

我将两者解析为对象,如下所示 myObject = mapper.readValue(SomeObject.get(0),myObject.class)

json 存储桶工作正常,而 xml 则不行。我要么需要将 XML 解析为与 json 类似的格式,要么用注释告诉它可以正确查看存储桶

我使用此 JSONObject jsonObject = XML.toJSONObject(cValue); 将 XML 解析为 json;

I have an api that can have its response in either XML or Json. For analyzing those results I created a single object that I want to populate for both json and xml. Problem is my XML parser seems to deserialize the XML into Json in a slightly different format.

In the middle of my response is a nested array
when I parse the JSON is looks like so

    "buckets":[
{"bucket":{"name":"soup","priority":10}},
{"bucket":{"name":"clams","priority":13}}],

And when I parse my XML to JSON it looks like so

"buckets":{"bucket":[
{"priority":"10","name":"soup"},
{"priority":"13","name":"clams"}]},

My single object created to hold this date has a private List buckets; Which the json parse handles fine while the xml parse throws an exception.

I am parsing both to the object like so
myObject = mapper.readValue(SomeObject.get(0),myObject.class)

The json buckets works fine the xml does not. I either need to parse the XML into similar format as the json or tell it maybe with an annotation to see buckets correctly

I parse my XML to json with this JSONObject jsonObject = XML.toJSONObject(cValue);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文