PDA

View Full Version : How to load JSON string into a form?



ragecf
13 Oct 2013, 7:39 PM
Hi, all

I want to load a JSON string to a form.

I got the string from back-end like:

root: [{item:"A", value:1}, {item:"B", value:2}, {item:"C", value:3}....]

How to format the return data to {"A":1, "B":2, "C":3} and load this string to a form?

Thanks.

evant
13 Oct 2013, 8:35 PM
var arr = Ext.decode(myString).root,
data = {};

Ext.Array.forEach(arr, function(item) {
data[item.item] = item.value;
});
console.log(data);

ragecf
13 Oct 2013, 10:46 PM
var arr = Ext.decode(myString).root,
data = {};

Ext.Array.forEach(arr, function(item) {
data[item.item] = item.value;
});
console.log(data);


Thanks.

And then how to load this data object to form?
I tried form.loadRecord(data) but failed.

ragecf
13 Oct 2013, 11:06 PM
var arr = Ext.decode(myString).root,
data = {};

Ext.Array.forEach(arr, function(item) {
data[item.item] = item.value;
});
console.log(data);


Got it.

form.getForm().setValues(data);

It's OK now. Thanks.