JSON 顺序混淆 [英] JSON order mixed up

查看:46
本文介绍了JSON 顺序混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试让我的页面按照我想要的顺序打印出 JSONObject 时遇到问题.在我的代码中,我输入了这个:

I've a problem trying to make my page printing out the JSONObject in the order i want. In my code, I entered this:

JSONObject myObject = new JSONObject();
myObject.put("userid", "User 1");
myObject.put("amount", "24.23");
myObject.put("success", "NO");

但是,当我看到页面上的显示时,它给出:

However, when I see the display on my page, it gives:

JSON 格式的字符串:[{"success":"NO", "userid":"User 1", "bid":24.23}]

JSON formatted string: [{"success":"NO", "userid":"User 1", "bid":24.23}]

我需要它的顺序是用户 ID,金额,然后是成功.已经尝试在代码中重新排序,但无济于事.我也试过 .append....在这里需要一些帮助谢谢!!

I need it in the order of userid, amount, then success. Already tried re-ordering in the code, but to no avail. I've also tried .append....need some help here thanks!!

推荐答案

您不能也不应该依赖 JSON 对象中元素的顺序.

You cannot and should not rely on the ordering of elements within a JSON object.

来自 https://www.json.org/

一个对象是一个无序的集合名称/值对

An object is an unordered set of name/value pairs

因此,JSON 库可以自由地重新排列元素的顺序,因为它们认为合适.这不是错误.

As a consequence, JSON libraries are free to rearrange the order of the elements as they see fit. This is not a bug.

这篇关于JSON 顺序混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆