sencha touch 2.4.1 - geting values of array sent via json string

273 views Asked by At

The following code works fine,

//sent from php

echo("({'title':'$myTitle', 'message':'$myMessage'})");

//in the ajax response bit in Control of sencha touch app

success: function(xhr,params)
{           
var data = Ext.JSON.decode(xhr.responseText.trim());
var aTitle=data.title;  //get the value of message, $myMessage
var aMessage=data.message;
console.log('Title:',aTitle,' - Message:',aMessage);
},

This does also but I cant get the individual data out however,

//from php, an array of data like before, a title and a message, only lots more of them

$data=json_encode($myArray);  
echo "({'myData':'$data'})";

//in the ajax response bit in Control of sencha touch app

success: function(xhr,params)
{           

var data = Ext.JSON.decode(xhr.responseText.trim());
var myArray=[];

Ext.Array.each(data.myData, function()
{
    myArray[count]= data.myData;    

    //this will spit out all the data from array.
    console.log('myArray contains:',myArray[count]);

    //but i wanted to select individual bit e.g.(pseudo code ish)
    myArray[0]=data.myData.title;

    or

    myArray[3]=data.myData.message; // that sort of thing

    count=count+1;          
 });
 },

In failing this could anyone tell me how to change this:

//get rid of non alphanumeric charecters myOutputString = myDataString.replace(/\W/g, '');

so it gets rid of the same exept - and , and .

then i can just ge the bits i want from data that way(but not the preffered option).

1

There are 1 answers

0
Sean Hayes On

L.

If you check out the api docs for Ext.Array here http://docs-origin.sencha.com/touch/2.4/2.4.1-apidocs/#!/api/Ext.Array

The callback to Ext.Array.each takes some arguments the first one is for the data you are after. Therefore I think you want something like the following.

Ext.Array.each(data.myData, function(data) {
 // Here data contains the data structure sent from your PHP code.
 console.log(data.title);
 console.log(data.message);
});

Hope that helps!