我有一个JSON对象,我在我的Java程序中构建.
JSONObject jObj = {"AAA:aaa","BBB:bbb","CCC:ccc"}
Run Code Online (Sandbox Code Playgroud)
我将此对象发送到服务器,在该服务器中,它期望JSON对象具有以下类型.
{"BBB:bbb", "AAA:aaa", "CCC:ccc"}
Run Code Online (Sandbox Code Playgroud)
我的问题是JSON对象的顺序在服务器端真的很重要吗?如果是,我该如何更改订单?
我正在使用JSONObject来删除我在 JSON 字符串中不需要的 certin 属性:
JSONObject jsonObject = new JSONObject(jsonString);
jsonObject.remove("owner");
jsonString = jsonObject.toString();
Run Code Online (Sandbox Code Playgroud)
它工作正常,但问题是 JSONObject 是“名称/值对的无序集合”,我想保持 String 在通过 JSONObject 操作之前的原始顺序。
知道如何做到这一点吗?
我有一个返回结构的远程CFC.它使用cfajaxproxy调用.我希望返回的JSON按顺序排列,即首先进入JSON对象的结构.但是,返回的JSON是混合顺序.
这是远程功能.
<cfcomponent displayname="validation" hint="">
<cffunction name="validateForm" displayname="validateForm" hint="" access="remote" verifyClient="yes" returntype="struct">
<cfargument name="formVals" type="struct" required="yes">
<cfset errors = StructNew()>
<cfif formVals.project neq "project">
<cfset errors["project"] = "Invalid project name." />
</cfif>
<cfif Len(formVals.description) eq 0>
<cfset errors["description"] = "Please enter a description." />
</cfif>
<cfif StructIsEmpty(errors)>
<cfset errors["message"]["type"] = "success">
<cfset errors["message"]["text"] = "Client and server-side validation passed successfully.">
<cfset errors["areErrors"] = false>
<cfelse>
<cfset errors["message"]["type"] = "validation">
<cfset errors["message"]["text"] = "Please fix the errors, and resubmit.">
<cfset errors["areErrors"] …Run Code Online (Sandbox Code Playgroud) 我需要创建常量json字符串或按键排序的json.常量json字符串是什么意思?请查看我创建的以下代码示例.
我的代码1:
public class GsonTest
{
class DataObject {
private int data1 = 100;
private String data2 = "hello";
}
public static void main(String[] args)
{
GsonTest obj=new GsonTest();
DataObject obj2 = obj.new DataObject();
Gson gson = new Gson();
String json = gson.toJson(obj2);
System.out.println(json);
}
}
Run Code Online (Sandbox Code Playgroud)
输出1:
{"data1":100,"data2":"hello"}
Run Code Online (Sandbox Code Playgroud)
我的代码2:
public class GsonTest
{
class DataObject {
private String data2 = "hello";
private int data1 = 100;
}
public static void main(String[] args)
{
GsonTest obj=new GsonTest();
DataObject obj2 = obj.new …Run Code Online (Sandbox Code Playgroud) 这是SpringMVC Controller代码片段:
@RequestMapping(value = "/getCityList", method = RequestMethod.POST)
public @ResponseBody LinkedHashMap<String, String> getCityList(@RequestParam(value = "countryCode") String countryCode, HttpServletRequest request) throws Exception {
//gets ordered city list of country [sorted by city name]
LinkedHashMap<String, String> cityList = uiOperationsService.getCityList(countryCode);
for (String s : cityList.values()) {
System.out.println(s); //prints sorted list [sorted by name]
}
return cityList;
}
Run Code Online (Sandbox Code Playgroud)
这是ajax调用:
function fillCityList(countryCode) {
$.ajax({
type: "POST",
url: '/getCityList',
data: {countryCode:countryCode},
beforeSend:function(){
$('#city').html("<option value=''>-- SELECT --</option>" );
}
}).done(function (data) {
console.log(data); // UNSORTED JSON STRING …Run Code Online (Sandbox Code Playgroud) 我有这两个JSON地图:
{
"1000006": "Alternate Business Phone",
"1000008": "Alternate Home Phone",
"1000001": "Business Phone",
"1000003": "Clinic Phone",
"3": "Facsimile",
"1000007": "Home Phone",
"1000004": "Lab Phone",
"4": "Pager",
"1000002": "Secure Msg Phone"
}
Run Code Online (Sandbox Code Playgroud)
和
{
"6": "Business Email",
"1000005": "Deliver To Email",
"7": "Personal Email"
}
Run Code Online (Sandbox Code Playgroud)
哪些是按值按字母顺序排列的键值映射.我正在使用这两个来根据另一个下拉列表的选定项目更改下拉菜单的内容.图片来说明:
电话选择:
电邮选择:
但是,正如您在图像中看到的那样,列表项的顺序不会被保留.
处理列表项的我的Javascript是这样的:
var options_1 = {"1000006":"Alternate Business Phone","1000008":"Alternate Home Phone","1000001":"Business Phone","1000003":"Clinic Phone","3":"Facsimile","1000007":"Home Phone","1000004":"Lab Phone","4":"Pager","1000002":"Secure Msg Phone"};
var options_2 = {"6":"Business Email","1000005":"Deliver To Email","7":"Personal Email"};
function changePhoneEmailItems()
{
var selectedItem = document.getElementById("addNewCategory").value; …Run Code Online (Sandbox Code Playgroud)