PDA

View Full Version : remove unnessary characters from json file using extjs



please
20 Aug 2012, 11:40 PM
hi,
any body please tell me how to remove unnessary charaters in json file using extjs

my code is
Ext.require([
'Ext.form.*'
//'Ext.layout.container.Column',
//'Ext.tab.Panel'
//'*'
]);

Ext.onReady(function() {
Ext.QuickTips.init();

var bd = Ext.getBody();


bd.createChild({tag: 'h2', html: 'Form 1 - Very Simple'});

var required = '<span style="color:red;font-weight:bold" data-qtip="Required">*</span>';

Ext.define('app.formStore', {
extend: 'Ext.data.Model',
fields: [
{name: 'mDNSServer1'},
{name: 'mDNSServer2'},
{name: 'mDefaultGateway'},
{name: 'mDuplexMode'},
{name: 'mIPAddress'},
{name: 'mLANSpeed'},
{name: 'mMode'},
{name: 'mSubnetMask'}
]
});

var myStore = Ext.create('Ext.data.Store', {
model: 'app.formStore',
proxy: {
type: 'ajax',
url : 'org.json',
reader:{
type:'json',


}
},
autoLoad:true,
listeners: {
load: function() {
//var form = Ext.getCmp('loginForm');
//alert(myStore.data);
//for(var i in myStore.data)
//console.log(i+'=='+myStore.data[i]);

//form.loadRecord(myStore.data.first());

var form = Ext.getCmp('loginForm');
form.getComponent('countryid').setValue( myStore.proxy.reader.jsonData.mDNSServer1);
form.getComponent('countrycode').setValue( myStore.proxy.reader.jsonData.mDNSServer2);
form.getComponent('countryname').setValue( myStore.proxy.reader.jsonData.mDefaultGateway);
form.loadRecord(myStore.data.first());
}

}});


var testForm = Ext.create('Ext.form.Panel', {
width: 500,
height:250,
renderTo: Ext.getBody(),
title: 'Country Form',
// model:'app.formStore',
id:'loginForm',
store:myStore,
waitMsgTarget: true,
fieldDefaults: {
labelAlign: 'right',
labelWidth: 85,
msgTarget: 'side'
},
//items: [{

// xtype: 'fieldset',
items: [
{
xtype:'textfield',
fieldLabel: 'ID',
id: 'countryid'
}, {
xtype:'textfield',
fieldLabel: 'CODE',
id: 'countrycode'
}, {
xtype:'textfield',
fieldLabel: 'COUNTRY',
id: 'countryname'
}]
// }]

});

org.json is:

d0 {"mDNSServer1":"172.26.0.2","mDNSServer2":"10.223.10.1","mDefaultGateway":"172.21.128.1","mDuplexMode":"Full","mIPAddress":"172.21.128.227","mLANSpeed":"100 Mbps","mMode":"dhcp","mSubnetMask":"255.255.254.0"} 0

i want to remove do starting of the json and also ending 0 how can i remove from extjs actually.i cannt change org.json its static i cannt change

vietits
21 Aug 2012, 12:56 AM
One of solutions is overriding getResponseData() of proxy reader:


var myStore = Ext.create('Ext.data.Store', {
model: 'app.formStore',
proxy: {
type: 'ajax',
url : 'org.json',
reader:{
type:'json',
getResponseData: function(response) {
var data, error;

try {
data = response.responseText || '';
if(data.match(/^[^{]*(\{.*\})[^}]*$/)){
data = RegExp.$1;
}
data = Ext.decode(data);
return this.readRecords(data);
} catch (ex) {
error = new Ext.data.ResultSet({
total : 0,
count : 0,
records: [],
success: false,
message: ex.message
});
this.fireEvent('exception', this, response, error);
Ext.Logger.warn('Unable to parse the JSON returned by the server');
return error;
}
}
}
},
...



If what you want is just load json data into a form, then you don't have to use a store, just use Ext.Ajax.request() as below:


Ext.Ajax.request({
url: 'org.json',
success: function(response){
var form = Ext.getCmp('loginForm');
var data = response.responseText || '';
if(data.match(/^[^{]*(\{.*\})[^}]*$/)){
data = RegExp.$1;
}
data = Ext.decode(data);
form.getComponent('countryid').setValue( data.mDNSServer1);
form.getComponent('countrycode').setValue( data.mDNSServer2);
form.getComponent('countryname').setValue( data.mDefaultGateway);
}
});

please
21 Aug 2012, 2:03 AM
thanks alot for u r response,with u r response i am successfully getting data from rest api.
and if i want to render it form.panel i given like in the code

xtype:'textfield',
fieldlable:'ID :',
id:'countryid'
so what i want is textfield means its giving to me textbox and placing data of json so instead of text field i want in the form of text like ID : 0.0.0.0

how can i do it.sorry to ask silly questions i am new to extjs i am unable to check in documentation how to use it.
please tell me how to know this properties