i'm not quite sure what a jag is - but you can use JSON.stringify() to convert an array of pairs or values into a JSON object
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
JSON.stringify(array)
Answer from eskimomatt on Stack Overflowi'm not quite sure what a jag is - but you can use JSON.stringify() to convert an array of pairs or values into a JSON object
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
JSON.stringify(array)
Since this is a java object, can you try this conversion using a java module that can convert the respective Object to JSON?.. You may have to import that module to the .jag (jaggery) file.
Script for backward-compatibility: https://github.com/douglascrockford/JSON-js/blob/master/json2.js
And call:
var myJsonString = JSON.stringify(yourArray);
Note: The JSON object is now part of most modern web browsers (IE 8 & above). See caniuse for full listing. Credit goes to: @Spudley for his comment below
I made it that way:
if I have:
var jsonArg1 = new Object();
jsonArg1.name = 'calc this';
jsonArg1.value = 3.1415;
var jsonArg2 = new Object();
jsonArg2.name = 'calc this again';
jsonArg2.value = 2.73;
var pluginArrayArg = new Array();
pluginArrayArg.push(jsonArg1);
pluginArrayArg.push(jsonArg2);
to convert pluginArrayArg (which is pure javascript array) into JSON array:
var jsonArray = JSON.parse(JSON.stringify(pluginArrayArg))
You cannot do this:
var objectArray= {};
objectArray[0]['title']='name1';
as objectArray[0] does not exist yet. There is no array at that index and therefor you cannot add a string at an index. You have do declare the array first. The rest of your code works just fine.
JSFIDDLE
var objectArray= [];
objectArray[0] = {}
objectArray[0]['title']='name1';
objectArray[0]['start']=new Date();
objectArray[1] = {}
objectArray[1]['title']='name2';
objectArray[1]['start']=new Date();
console.log(JSON.stringify(objectArray));โ
hai i got my output in JSON...now i need to convert those data into javascript..
Use JSON.parse() function in order to convert it to JS object.
var obj = JSON.parse(yourJsonString);
And now you can use for-in loop to iterate over each of its items:
for (var x in obj){
if (obj.hasOwnProperty(x)){
// your code
}
}
If you are using jQuery you can use
var object = $.parseJSON(jsonstring);
Or add this library https://raw.github.com/douglascrockford/JSON-js/master/json2.js and give
var object = JSON.parse(jsonstring);
You can use the GSON library to accomplish this.
ArrayList<String> mylist = new ArrayList<String> ();
mylist.add("abc");
mylist.add("cfd");
mylist.add("ert");
mylist.add("fg");
mylist.add("ujk");
String json = new Gson().toJson(mylist);
You can refer to the GSON User Guide for more support.
JSONArray jsArray = new JSONArray(mylist);
Or
Gson gson = new Gson();
String jsonArray = gson.toJson(mylist);
Get java-json.jar and Gson.jar here
If I read the JSONArray constructors correctly, you can build them from any Collection (arrayList is a subclass of Collection) like so:
ArrayList<String> list = new ArrayList<String>();
list.add("foo");
list.add("baar");
JSONArray jsArray = new JSONArray(list);
References:
- jsonarray constructor: http://developer.android.com/reference/org/json/JSONArray.html#JSONArray%28java.util.Collection%29
- collection: http://developer.android.com/reference/java/util/Collection.html
Use Gson library to convert ArrayList to JsonArray.
Gson gson = new GsonBuilder().create();
JsonArray myCustomArray = gson.toJsonTree(myCustomList).getAsJsonArray();
users is already a JS object (not JSON). But here you go:
var users_array = [];
for(var i in users) {
if(users.hasOwnProperty(i) && !isNaN(+i)) {
users_array[+i] = users[i];
}
}
Edit: Insert elements at correct position in array. Thanks @RoToRa.
Maybe it is easier to not create this kind of object in the first place. How is it created?
Just for fun - if you know the length of the array, then the following will work (and seems to be faster):
users.length = 3;
users = Array.prototype.slice.call(users);
Simply use JSON.stringify() to convert your array to json string
var jsonString = JSON.stringify(yourArray);
You need to declare the values and push that onto a tmp variable before pushing that index onto the final/main object
var tmp_values, jsonObj = []; //declare object
for (var i=1;i<csvAsArray.length;i++) {
var tmp_values = [];
for (var l=1;l<csvAsArray[0].length;l++) {
tmp_values.push({label: csvAsArray[0][l], value: csvAsArray[i][l]}); //label + value respectively
}
jsonObj.push({key: csvAsArray[i][0], values: tmp_values}); //key
}