1. #1
    Ext User
    Join Date
    Dec 2008
    Posts
    49
    Vote Rating
    0
    D-L is on a distinguished road

      0  

    Default Handling JSON with special characters

    Handling JSON with special characters


    Hello guys, can anyone help me on this please. I really need it badly.
    Here is a sample json file produced by a webservice.

    {
    "@list":[
    {
    "@IndividualSummary":{
    "@userId":"959465",
    "@userName":"Martin",
    "@transCnt":"2",
    "@stationCode":"HL"
    },
    {
    "@userId":"959466",
    "@userName":"Dennis",
    "@transCnt":"5",
    "@stationCode":"ML"
    }
    }
    ]
    }

    the problem is I can't mapped it with the "@" sign at the beginning of every word, if I create a dummy json based on this one and I remove the "@" signs, its a success. but with the "@" sign is the problem.the dummy json file that I create without the "@", I display it like this

    var
    user=store.getAt(0).get('userName');
    alert(user);====>this works if i remove the "@" sign manually.
    can anyone help me on how to handle this with the "@" sign. please. for example i want to get the username "Dennis" and display it in an alert message. please guys I really need this. Thank you very much in advance


  2. #2
    Sencha - Ext JS Dev Team evant's Avatar
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    16,660
    Vote Rating
    584
    evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute

      0  

    Default


    You can't, JSON has to be valid javascript.
    Evan Trimboli
    Sencha Developer
    Twitter - @evantrimboli
    Don't be afraid of the source code!

  3. #3
    Sencha - Community Support Team Condor's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    24,246
    Vote Rating
    83
    Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of

      0  

    Default


    That is not valid JSON (use jslint.com to check).

    Assuming your JSON would be:
    Code:
    {"@list":[
    	{"@userId":"959465", "@userName":"Martin", "@transCnt":"2", "@stationCode":"HL"},
    	{"@userId":"959466", "@userName":"Dennis", "@transCnt":"5", "@stationCode":"ML"}
    ]}
    Then you could use the following store/reader config:
    Code:
    var store = new Ext.data.JsonStore({
    	url: '...',
    	root: '@list',
    	id: '@userId',
    	fields: [
    		{name: 'userId', mapping: '@userId'},
    		{name: 'userName', mapping: '@userName'},
    		{name: 'transCnt', mapping: '@transCnt'},
    		{name: 'stationCode', mapping: '@stationCode'}
    	]
    });

  4. #4
    Ext User
    Join Date
    Dec 2008
    Posts
    49
    Vote Rating
    0
    D-L is on a distinguished road

      0  

    Default JSON with special characters

    JSON with special characters


    Thanks evant, I really appreciate it. Can anyone else help me with this please, I really have to do this. Is there a way to delete the special characters first and then have the json reader read it after? Thanks again evant.

  5. #5
    Ext User
    Join Date
    Dec 2008
    Posts
    49
    Vote Rating
    0
    D-L is on a distinguished road

      0  

    Default JSON with special characters

    JSON with special characters


    Thanks condor, I was able to use your advice. thank you very very much. just a follow up question.you gave this as an example right?

    {"@list":[
    {"@userId":"959465", "@userName":"Martin", "@transCnt":"2", "@stationCode":"HL"},
    {"@userId":"959466", "@userName":"Dennis", "@transCnt":"5", "@stationCode":"ML"}
    ]}

    What if theres a field between username and trancnt? and it goes into a deeper area? like this for example?

    {"@list":[
    {"@userId":"959465", "@userName":"Martin", "nickName":{"@nick":"mart"}, "@transCnt":"2", "@stationCode":"HL"},
    {"@userId":"959466", "@userName":"Dennis", "nickName":{"@nick":"denz"}, "@transCnt":"5", "@stationCode":"ML"}
    ]}

    how do you trace the nickname part if its 2nd level deeper? Please help me on this.

  6. #6
    Sencha - Ext JS Dev Team Animal's Avatar
    Join Date
    Mar 2007
    Location
    Notts/Redwood City
    Posts
    30,496
    Vote Rating
    44
    Animal has a spectacular aura about Animal has a spectacular aura about Animal has a spectacular aura about

      0  

  7. #7
    Ext User
    Join Date
    Dec 2008
    Posts
    49
    Vote Rating
    0
    D-L is on a distinguished road

      0  

    Default JSON with special characters

    JSON with special characters


    Sir animal, thank you for the help, but from how i understand, convert function converts value which are already provided by the reader and to be stored in the record. the problem is I don't think the json with "@" sign is read by the json reader when the field is in the 2nd layer, like the nickname for example.

  8. #8
    Sencha - Ext JS Dev Team Animal's Avatar
    Join Date
    Mar 2007
    Location
    Notts/Redwood City
    Posts
    30,496
    Vote Rating
    44
    Animal has a spectacular aura about Animal has a spectacular aura about Animal has a spectacular aura about

      0  

    Default


    Read the linked docs.

  9. #9
    Sencha - Community Support Team Condor's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    24,246
    Vote Rating
    83
    Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of

      0  

    Default


    Example:
    Code:
    {name: 'nick', mapping: 'nickName', convert: function(v){return v['@nick'];}},

  10. #10
    Ext User
    Join Date
    Dec 2008
    Posts
    49
    Vote Rating
    0
    D-L is on a distinguished road

      0  

    Default JSON with special characters

    JSON with special characters


    Thanks guys, specially animal and condor, you guys are the Ext Masters. Thank you very much.

Thread Participants: 3