简体   繁体   中英

How to create JSON array in java with non-string key names?

I am using this bootstrap transfer plugin at UI which i have to populate with the help of data sent from java application. This plugin requires the data to be an array of objects with 'value' and 'content' properties. I can easily create a list in java and convert it to JSON array, but the problem here is that this plugin requires key names as non string names. I tried using string key names but that just didn't worked. I looked up for ways to create non-string key names in JSON and the only way i could find was to write my own parser, and that was also not recommended. So how can i prepare my data in java for this plugin ??

Edit: As mentioned in the plugin documentation, here is a sample data to populate it.

$(function() {
...
var t = $('#test').bootstrapTransfer();
t.populate([
    {value:"1", content:"Apple"},
    {value:"2", content:"Orange"},
    {value:"3", content:"Banana"},
    {value:"4", content:"Peach"},
    {value:"5", content:"Grapes"}
]);
...

});

When i prepare a JSON array, it is like {"value":"1", "content":"Apple"} which doesn't work for this plugin.

You might check how it runs again when you use a quoted string. When accessing a JSON object, the quotes for keys are not needed. Take for example this code (in Nodejs):

var console = require("console");
var data = { "value1" : 13,
    "value2" : "hello",
    value3: 15,
    value4 : "hello again" };

console.log("Value 1 = " + data.value1 );
console.log("Value 2 = " + data.value2 );
console.log("Value 3 = " + data.value3 );
console.log("Value 4 = " + data.value4 );

Some of the object is declared with quoted strings, and some are not. All are accessed without the quotes, and my console shows:

Value 1 = 13
Value 2 = hello
Value 3 = 15
Value 4 = hello again

So it really shouldn't matter how your keys are defined in java. I know that isn't an exact answer to your question of how to do it, but you really shouldn't need to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM