1. #1
    Sencha User
    Join Date
    Jan 2013
    Location
    Germany, Freiburg i. Br.
    Posts
    31
    Vote Rating
    8
    StudentDaniel will become famous soon enough

      0  

    Default Howto Object within JSON-Array convert

    Howto Object within JSON-Array convert


    Hi Sencha-Friends :-)

    I have trouble to use something like this:

    PHP Code:
    [
        {
            
    "errorCode"0,
            
    "state""ok",
             
    "code":123456
        
    }

    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.

    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:

    PHP Code:
    Ext.define("Modelname", {     
       
    extend"Ext.data.Model",    
       
    config: {         
       
    fields: [            
           
    "code",             
           
    "state",
           
    "errorCode"         
           
    ]     
        } 
    });  

    var 
    storetest Ext.create("Ext.data.Store", {    
       
    model"Modelname",     
       
    autoLoadtrue,     
       
    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(); 
    If I dump this by console.log(store) I get a Store with the Model but without data.

    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
    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 ?

    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...

  2. #2
    Sencha Premium Member
    Join Date
    May 2010
    Location
    Guatemala, Central America
    Posts
    1,266
    Vote Rating
    80
    ssamayoa is a jewel in the rough ssamayoa is a jewel in the rough ssamayoa is a jewel in the rough ssamayoa is a jewel in the rough

      0  

    Default


    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 3.x / ExtJS 4 & 5
    Server side: JEE / EJB 3.x / CDI / JPA 2.x/ JAX-RS / JasperReports
    Application Server: Glassfish / WildFly
    Databases: Oracle / DB2 / MySQL / Firebird

    If you like my answer please vote!

  3. #3
    Sencha User
    Join Date
    Jan 2013
    Location
    Germany, Freiburg i. Br.
    Posts
    31
    Vote Rating
    8
    StudentDaniel will become famous soon enough

      0  

    Default


    Quote Originally Posted by ssamayoa View Post
    BTW, where are you located?
    updated :-)

  4. #4
    Sencha User
    Join Date
    Jan 2013
    Location
    Germany, Freiburg i. Br.
    Posts
    31
    Vote Rating
    8
    StudentDaniel will become famous soon enough

      0  

    Default 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 ...


    Please post the exact response you get from server, I mean the string.
    I don't know what you mean by "string" but I mad a screenshot of Safari-Console after:
    Code:
    console.log(storetest);
    console.jpg

    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 :-)

  5. #5
    Sencha Premium Member
    Join Date
    May 2010
    Location
    Guatemala, Central America
    Posts
    1,266
    Vote Rating
    80
    ssamayoa is a jewel in the rough ssamayoa is a jewel in the rough ssamayoa is a jewel in the rough ssamayoa is a jewel in the rough

      0  

    Default


    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 3.x / ExtJS 4 & 5
    Server side: JEE / EJB 3.x / CDI / JPA 2.x/ JAX-RS / JasperReports
    Application Server: Glassfish / WildFly
    Databases: Oracle / DB2 / MySQL / Firebird

    If you like my answer please vote!

  6. #6
    Sencha User
    Join Date
    Jan 2013
    Location
    Germany, Freiburg i. Br.
    Posts
    31
    Vote Rating
    8
    StudentDaniel will become famous soon enough

      0  

    Default


    This is the response of the Server for the given request
    But this isn't the question this is the Client side challange :-)

    Code:
    [
        {
            "errorCode": 0,
            "state": "ok",
             "code":123456
        }
    ]
    Please Note: i cannot change this on Server side.
    Last edited by StudentDaniel; 8 Feb 2013 at 1:23 PM. Reason: Send by mobil with very Big Fingers ;-)

  7. #7
    Sencha Premium Member
    Join Date
    May 2010
    Location
    Guatemala, Central America
    Posts
    1,266
    Vote Rating
    80
    ssamayoa is a jewel in the rough ssamayoa is a jewel in the rough ssamayoa is a jewel in the rough ssamayoa is a jewel in the rough

      1  

    Default


    And you get just one record or can be several?
    For example:

    Code:
    [
        {
            "errorCode": 0,
            "state": "ok",
             "code":123456
        },
       
        {
            "errorCode": 0,
            "state": "ok",
             "code":666
        },
        
    ]
    If you expect only one record you should retrieve using Ext.Ajax.request() (http://docs.sencha.com/ext-js/4-1/#!...method-request)

    If is a record list you have to write your own reader.

    Regards.
    UI: Sencha Architect 3.x / ExtJS 4 & 5
    Server side: JEE / EJB 3.x / CDI / JPA 2.x/ JAX-RS / JasperReports
    Application Server: Glassfish / WildFly
    Databases: Oracle / DB2 / MySQL / Firebird

    If you like my answer please vote!

  8. #8
    Sencha User
    Join Date
    Jan 2013
    Location
    Germany, Freiburg i. Br.
    Posts
    31
    Vote Rating
    8
    StudentDaniel will become famous soon enough

      0  

    Default


    If you expect only one record you should retrieve using Ext.Ajax.request()
    is an Ajax-Request not limited to the same Domain?

    I tried this before and reproduced this:

    Code:
    ...is not allowed by Access-Control-Allow-Origin.
    Also I've done this via Ext.Ajax shown in the initial Forum-Post:

    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   ]
    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.

    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 :-)

  9. #9
    Sencha Premium Member
    Join Date
    May 2010
    Location
    Guatemala, Central America
    Posts
    1,266
    Vote Rating
    80
    ssamayoa is a jewel in the rough ssamayoa is a jewel in the rough ssamayoa is a jewel in the rough ssamayoa is a jewel in the rough

      1  

    Default


    You supposed to write a reader. I think so too. But what I have to do?
    Seems a daunting task but read and understand what reader does (looking at ExtJS sources of course) and juts do it.

    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.JsonP
    UI: Sencha Architect 3.x / ExtJS 4 & 5
    Server side: JEE / EJB 3.x / CDI / JPA 2.x/ JAX-RS / JasperReports
    Application Server: Glassfish / WildFly
    Databases: Oracle / DB2 / MySQL / Firebird

    If you like my answer please vote!

  10. #10
    Sencha User
    Join Date
    Jan 2013
    Location
    Germany, Freiburg i. Br.
    Posts
    31
    Vote Rating
    8
    StudentDaniel will become famous soon enough

      0  

    Default First Challange pased :-)

    First Challange pased :-)


    Hi :-)

    thanks for your hole feedback.

    Here I will give back my first results:

    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); 
    This will result in Safari-Console like this:

    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 :-)

Thread Participants: 1

Tags for this Thread

Turkiyenin en sevilen filmlerinin yer aldigi xnxx internet sitemiz olan ve porn sex tarzi bir site olan mobil porno izle sitemiz gercekten dillere destan bir durumda herkesin sevdigi bir site olarak tarihe gececege benziyor. Sitenin en belirgin ozelliklerinden birisi de Turkiyede gercekten kaliteli ve muntazam, duzenli porno izle siteleri olmamasidir. Bu yuzden iste. Ayrica en net goruntu kalitesine sahip adresinde yayinlanmaktadir. Mesela diğer sitelerimizden bahsedecek olursak, en iyi hd porno video arşivine sahip bir siteyiz. "The Best anal porn videos and slut anus, big asses movies set..." hd porno faketaxi