-
8 Feb 2013 8:32 AM #1
Howto Object within JSON-Array convert
Howto Object within JSON-Array convert
Hi Sencha-Friends :-)
I have trouble to use something like this:
In every tutorial and documentation there are used something like this:PHP Code:[
{
"errorCode": 0,
"state": "ok",
"code":123456
}
]
So I am searching for a way to convert this without regex.PHP Code:{
"errorCode": 0,
"state": "ok",
"code":123456
}
This is a JSON-Answer of a Rest-Webservice - so I cannot change this on servers side.
Because of there are a lot of responses like this, I need a way to handle this within a json-reader?
This is what I tried:
If I dump this by console.log(store) I get a Store with the Model but without data.PHP Code:Ext.define("Modelname", {
extend: "Ext.data.Model",
config: {
fields: [
"code",
"state",
"errorCode"
]
}
});
var storetest = Ext.create("Ext.data.Store", {
model: "Modelname",
autoLoad: true,
storeId: "usersStore",
proxy: {
type: "rest",
url : "webservice.example.com/index.php",
params: {
apiKey:'2345',
code:123456,
action:'code'
},
reader: {
type: "json",
rootProperty: "" // because of [ {"foo":"bar"}] ... dosn't work
}
}
});
storetest.load();
Loading data another way - local by ajax from url: 'app/data/test.json' I will get something like this:
I know, there has to be a mistace within the first try above (maybe a additional helpful tip ?) but how can I transform the Array with Object in it to a nativ and usable Object ?Code:[ ] Object code: "123456" errorCode: 0 state: "ok" __proto__: Object
The Extjs 4 API gives a toArray() Method but no toObject() Method. Also I don't know how to traverse this null like node..
So I think I nead to find a nativ javascript way to do this. There are some ways I tried now - but without success. Do you know a practical way?
So I need:
1) a way to clean the array to a real object
2) the method to override because of every response will have the same Array-Brackets arround the native object...
-
8 Feb 2013 9:40 AM #2
I'm not quite sure if I understand what you want...
Please post the exact response you get from server, I mean the string.
BTW, the expected raw response json reader expect is something like this:
{
data: [{a:1, b:1}, {a:2, b:2}, {a:3, b:3}]
total: 3,
message: "ok",
success: true
}
root property ("data" in this example) is mandatory and tells to the reader where to find record list of the store.
BTW, where are you located?
Regards.UI: Sencha Architect 2.x / ExtJS 4 MVC
Server side: EJB 3.1 / CDI / JPA 2 / JAX-RS / JasperReports
Application Server: Glassfish 3.1.x
Databases: Oracle 10g & 11g / DB2 9 & 10 / Firebird 2.5
If you like my answer please vote!
-
8 Feb 2013 10:45 AM #3
-
8 Feb 2013 10:53 AM #4
I get crazy with this Forum-Software - I wrote this Reply the third time ...
I get crazy with this Forum-Software - I wrote this Reply the third time ...
I don't know what you mean by "string" but I mad a screenshot of Safari-Console after:Please post the exact response you get from server, I mean the string.
console.jpgCode:console.log(storetest);
The other facts about root property and expected Data-Struct I know ... and it makes me crazy :-)
I firstly want to come over the [ ] problem - how can I parse this away. The next: finding the right Method to override to modify the Store-Class to do this for this special Webservice I developer against :-)
-
8 Feb 2013 12:03 PM #5
When I said string I'm referring to the text responded by the server:
Selection_003.png
Can you post it here?UI: Sencha Architect 2.x / ExtJS 4 MVC
Server side: EJB 3.1 / CDI / JPA 2 / JAX-RS / JasperReports
Application Server: Glassfish 3.1.x
Databases: Oracle 10g & 11g / DB2 9 & 10 / Firebird 2.5
If you like my answer please vote!
-
8 Feb 2013 1:18 PM #6
This is the response of the Server for the given request
But this isn't the question this is the Client side challange :-)
Please Note: i cannot change this on Server side.Code:[ { "errorCode": 0, "state": "ok", "code":123456 } ]Last edited by StudentDaniel; 8 Feb 2013 at 1:23 PM. Reason: Send by mobil with very Big Fingers ;-)
-
8 Feb 2013 1:31 PM #7
And you get just one record or can be several?
For example:
If you expect only one record you should retrieve using Ext.Ajax.request() (http://docs.sencha.com/ext-js/4-1/#!...method-request)Code:[ { "errorCode": 0, "state": "ok", "code":123456 }, { "errorCode": 0, "state": "ok", "code":666 }, ]
If is a record list you have to write your own reader.
Regards.UI: Sencha Architect 2.x / ExtJS 4 MVC
Server side: EJB 3.1 / CDI / JPA 2 / JAX-RS / JasperReports
Application Server: Glassfish 3.1.x
Databases: Oracle 10g & 11g / DB2 9 & 10 / Firebird 2.5
If you like my answer please vote!
-
8 Feb 2013 2:11 PM #8
is an Ajax-Request not limited to the same Domain?If you expect only one record you should retrieve using Ext.Ajax.request()
I tried this before and reproduced this:
Also I've done this via Ext.Ajax shown in the initial Forum-Post:Code:...is not allowed by Access-Control-Allow-Origin.
The reason I leave the Way of Ext.Ajax is there where no way to do Requests to foreign URLs .. so I went back to the Store/Model/Reader Way.Loading data another way - local by ajax from url: 'app/data/test.json' I will get something like this:
Code:[ Object code: "123456" errorCode: 0 state: "ok" __proto__: Object ]
You supposed to write a reader. I think so too. But what I have to do?
I think I have to use a JSON-Reader where to override a single Method where reading the response.
And at this Point I don't know how to handle the [ .... ] outside the Object. I want to make a native JSON-Object from a Array with one Object...
Do you know a native Javascript-Way? So I could build something and leave it here for following Searchers :-)
-
8 Feb 2013 2:30 PM #9
Seems a daunting task but read and understand what reader does (looking at ExtJS sources of course) and juts do it.You supposed to write a reader. I think so too. But what I have to do?
According to this http://stackoverflow.com/questions/8...t-ajax-request you can use http://docs.sencha.com/ext-js/4-1/#!/api/Ext.data.JsonPUI: Sencha Architect 2.x / ExtJS 4 MVC
Server side: EJB 3.1 / CDI / JPA 2 / JAX-RS / JasperReports
Application Server: Glassfish 3.1.x
Databases: Oracle 10g & 11g / DB2 9 & 10 / Firebird 2.5
If you like my answer please vote!
-
9 Feb 2013 8:24 AM #10
First Challange pased :-)
First Challange pased :-)
Hi :-)
thanks for your hole feedback.
Here I will give back my first results:
This will result in Safari-Console like this:PHP Code:// transform response to string and regex away [ ] finaly trim whitespaces ...
var data = response.responseText.toString().replace(/^[\[]/,"").replace(/[\]]$/,"").trim();
// take a look at layout
console.log(data);
// pares like JSON to get an Object
var data = JSON.parse(data);
console.log('Success: ')
console.log(data);
firstresult.jpg
The wanted replace("/^[\[]|[\]]$/",""); dosn't work ..?!? .. but I have a first success :-)
Next I will override the function within the reader. I will give it back too :-)


Reply With Quote