Results 1 to 7 of 7

Thread: JsonReader data parsing

  1. #1
    Sencha User
    Join Date
    Feb 2010
    Posts
    12

    Default JsonReader data parsing

    Can someone help me with parsing JSON data. the data is formstted like this:
    PHP Code:
     
    [
    {
    "v":1},
    {
    "s":0,"m":"KN736"},
    {
    "t":2,"i":1,
    "f":[{"f":25,"v":"USD/CAD"},{"f":28,"v":"1.0539"},{"f":27,"v":"1.0293"},
    {
    "f":30,"v":"1.0772"},{"f":29,"v":"1.0514"},{"f":21,"v":"1.1124"},{"f":22,"v":"0.9928"},{"f":19,"v":"1.0529"},{"f":43,"v":"1.0852"},
    {
    "f":44,"v":"0.9928"},{"f":2,"v":"1.0181"},{"f":3,"v":"1.014"},{"f":4,"v":"1.0168"},{"f":10,"v":"1.0177"},{"f":11,"v":"1.01812"},
    {
    "f":16,"v":"1.01791"},{"f":17,"v":"1281090702"},{"f":23,"v":"MIGF"},{"f":1,"v":"1.0168"},{"f":47,"v":"0.38"},{"f":48,"v":"0.395"},{"f":51,"v":"0.12"},
    {
    "f":52,"v":"0.13"},{"f":53,"v":"0.85"},{"f":54,"v":"0.95"},{"f":55,"v":"1.7"},{"f":56,"v":"1.85"},{"f":57,"v":"2.55"},{"f":58,"v":"2.75"},{"f":59,"v":"3.7"},
    {
    "f":60,"v":"4.0"},{"f":61,"v":"9.8"},{"f":62,"v":"10.0"},{"f":63,"v":"15.7"},{"f":64,"v":"16.3"},{"f":65,"v":"22.4"},{"f":66,"v":"23.2"},{"f":67,"v":"29.9"},
    {
    "f":68,"v":"30.9"},{"f":69,"v":"38.0"},{"f":70,"v":"39.5"},{"f":71,"v":"44.75"},{"f":72,"v":"46.75"},{"f":73,"v":"54.25"},{"f":74,"v":"56.25"},
    {
    "f":75,"v":"62.5"},{"f":76,"v":"65.5"},{"f":77,"v":"71.0"},{"f":78,"v":"74.0"},{"f":79,"v":"80.25"},{"f":80,"v":"83.25"},{"f":81,"v":"89.0"},{"f":82,"v":"93.0"},
    {
    "f":83,"v":"192.0"},{"f":84,"v":"207.0"},{"f":85,"v":"299.0"},{"f":86,"v":"319.0"},{"f":87,"v":"384.0"},{"f":88,"v":"414.0"},{"f":89,"v":"423.0"},{"f":90,"v":"473.0"}]},
     
    {
    "t":2,"i":2,
    "f":[{"f":21,"v":"1.5144"},{"f":22,"v":"1.18757"},{"f":43,"v":"1.45791"},{"f":44,"v":"1.18757"},{"f":8,"v":"1000.00"},{"f":5,"v":"217747104.00"},{"f":25,"v":"EUR/USD"},
    {
    "f":2,"v":"1.32034"},{"f":3,"v":"1.31571"},{"f":4,"v":"1.31794"},{"f":10,"v":"1.31645"},{"f":11,"v":"1.31652"},{"f":17,"v":"1281090709"},{"f":23,"v":"GSGI"},{"f":1,"v":"1.31788"},
    {
    "f":19,"v":"1.43032"},{"f":27,"v":"1.30495"},{"f":28,"v":"1.26221"},{"f":29,"v":"1.2654"},{"f":30,"v":"1.43704"}]}

    I want to load the values containd in each row that start with f:


    PHP Code:
    "f":[{"f":21,"v":"1.5144"},{"f":22,"v":"1.18757"},{"f":43,"v":"1.45791"},{"f":44,"v":"1.18757"},{"f":8,"v":"1000.00"},{"f":5,"v":"217747104.00"},{"f":25,"v":"EUR/USD"},
    {
    "f":2,"v":"1.32034"},{"f":3,"v":"1.31571"},{"f":4,"v":"1.31794"},{"f":10,"v":"1.31645"},{"f":11,"v":"1.31652"},{"f":17,"v":"1281090709"},{"f":23,"v":"GSGI"},{"f":1,"v":"1.31788"},
    {
    "f":19,"v":"1.43032"},{"f":27,"v":"1.30495"},{"f":28,"v":"1.26221"},{"f":29,"v":"1.2654"},{"f":30,"v":"1.43704"}]}

    and

    PHP Code:
    "f":[{"f":25,"v":"USD/CAD"},{"f":28,"v":"1.0539"},{"f":27,"v":"1.0293"},
    {
    "f":30,"v":"1.0772"},{"f":29,"v":"1.0514"},{"f":21,"v":"1.1124"},{"f":22,"v":"0.9928"},{"f":19,"v":"1.0529"},{"f":43,"v":"1.0852"},
    {
    "f":44,"v":"0.9928"},{"f":2,"v":"1.0181"},{"f":3,"v":"1.014"},{"f":4,"v":"1.0168"},{"f":10,"v":"1.0177"},{"f":11,"v":"1.01812"},
    {
    "f":16,"v":"1.01791"},{"f":17,"v":"1281090702"},{"f":23,"v":"MIGF"},{"f":1,"v":"1.0168"},{"f":47,"v":"0.38"},{"f":48,"v":"0.395"},{"f":51,"v":"0.12"},
    {
    "f":52,"v":"0.13"},{"f":53,"v":"0.85"},{"f":54,"v":"0.95"},{"f":55,"v":"1.7"},{"f":56,"v":"1.85"},{"f":57,"v":"2.55"},{"f":58,"v":"2.75"},{"f":59,"v":"3.7"},
    {
    "f":60,"v":"4.0"},{"f":61,"v":"9.8"},{"f":62,"v":"10.0"},{"f":63,"v":"15.7"},{"f":64,"v":"16.3"},{"f":65,"v":"22.4"},{"f":66,"v":"23.2"},{"f":67,"v":"29.9"},
    {
    "f":68,"v":"30.9"},{"f":69,"v":"38.0"},{"f":70,"v":"39.5"},{"f":71,"v":"44.75"},{"f":72,"v":"46.75"},{"f":73,"v":"54.25"},{"f":74,"v":"56.25"},
    {
    "f":75,"v":"62.5"},{"f":76,"v":"65.5"},{"f":77,"v":"71.0"},{"f":78,"v":"74.0"},{"f":79,"v":"80.25"},{"f":80,"v":"83.25"},{"f":81,"v":"89.0"},{"f":82,"v":"93.0"},
    {
    "f":83,"v":"192.0"},{"f":84,"v":"207.0"},{"f":85,"v":"299.0"},{"f":86,"v":"319.0"},{"f":87,"v":"384.0"},{"f":88,"v":"414.0"},{"f":89,"v":"423.0"},{"f":90,"v":"473.0"}] 
    thank you

  2. #2
    Sencha User Condor's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    24,245

    Default

    You could use:
    Code:
    root: function(root){
      // Combine the f[]'s from all objects in one single array
      var data = [], f;
      for(var i = 0, len = root.length; i < len; i++) {
        if (f = root[i].f) {
          data.push.apply(data, f);
        }
      }
      return data;
    },
    idProperty: 'f', // assuming f is unique
    fields: ['f', 'v']
    (or do you also need to check if every object inside f also contains f?

  3. #3
    Sencha User
    Join Date
    Feb 2010
    Posts
    12

    Default

    Hi Condor,

    Thank you very much, the code works great.
    It returns one object for each of the f's in each big f.
    What I need is, in the example above 2 objects: each one of these two has as properties the f's inside the big f and as values for these propeties the v's values. Something like:

    PHP Code:
    //object1
    object1.f25 'EUR/USD';
    object1.f21 '1.5144';
    //............... and so on
     
    //object1
    object2.f25 'USD/CAD';
    //.............and so on 
    Thank you.

  4. #4
    Sencha User Condor's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    24,245

    Default

    So you want to load two stores with:
    Code:
    root: function(root){
      for(var i = 0, len = root.length; i < len; i++) {
        if (root[i].v == 1) {
          return root[i].f;
        }
      }
    }
    and
    Code:
    root: function(root){
      for(var i = 0, len = root.length; i < len; i++) {
        if (root[i].v == 2) {
          return root[i].f;
        }
      }
    }

  5. #5
    Sencha User
    Join Date
    Feb 2010
    Posts
    12

    Default

    Hi Condor,

    Thank you very much.
    What i need is one store wich in this case contains 2 records(from the json in the example above): object1 and object2 as described above, which are retrieved from the big json I receive(of course, the number of objects in json can vary). As a observation that can be useful, each "i" above the big "f" is unique. Is that possible?
    thank you!

  6. #6
    Sencha User Condor's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    24,245

    Default

    So you want:
    Code:
    root: function(root){
      // Only return objects with an f property
      var data = [];
      for(var i = 0, len = root.length; i < len; i++) {
        if (root[i].f) {
          data.push(root[i]);
        }
      }
      return data;
    },
    idProperty: 'i', // assuming i is unique
    fields: ['t', 'i', 'f']

  7. #7
    Sencha User
    Join Date
    Feb 2010
    Posts
    12

    Default

    Thank you very much, Condor. That's what I wanted. I made a little modification and it works great.

Similar Threads

  1. JSONstore not parsing(?) data correctly to display in grid
    By tylerbell in forum Ext 3.x: Help & Discussion
    Replies: 0
    Last Post: 19 Jul 2010, 7:43 AM
  2. JsonReader problem with parsing international characters
    By Keshav78 in forum Ext 2.x: Help & Discussion
    Replies: 4
    Last Post: 22 Aug 2008, 4:46 AM
  3. Parsing param to Ext.data.Store
    By ansis_si in forum Ext 2.x: Help & Discussion
    Replies: 1
    Last Post: 10 May 2008, 1:19 PM
  4. JsonReader date rendering/parsing problem
    By tuxz0r in forum Ext 2.x: Help & Discussion
    Replies: 1
    Last Post: 16 Apr 2008, 11:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •