Hybrid View

  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,306
    Vote Rating
    109
    ssamayoa is just really nice ssamayoa is just really nice ssamayoa is just really nice ssamayoa is just really nice

      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,306
    Vote Rating
    109
    ssamayoa is just really nice ssamayoa is just really nice ssamayoa is just really nice ssamayoa is just really nice

      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,306
    Vote Rating
    109
    ssamayoa is just really nice ssamayoa is just really nice ssamayoa is just really nice ssamayoa is just really nice

      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!

Thread Participants: 1

Tags for this Thread