PDA

View Full Version : JsonReader data parsing



vicentiu
6 Aug 2010, 3:15 AM
Can someone help me with parsing JSON data. the data is formstted like this:



[
{"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:




"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



"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

Condor
6 Aug 2010, 3:22 AM
You could use:

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?

vicentiu
6 Aug 2010, 5:01 AM
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:



//object1
object1.f25 = 'EUR/USD';
object1.f21 = '1.5144';
//............... and so on

//object1
object2.f25 = 'USD/CAD';
//.............and so on


Thank you.

Condor
6 Aug 2010, 5:06 AM
So you want to load two stores with:

root: function(root){
for(var i = 0, len = root.length; i < len; i++) {
if (root[i].v == 1) {
return root[i].f;
}
}
}
and

root: function(root){
for(var i = 0, len = root.length; i < len; i++) {
if (root[i].v == 2) {
return root[i].f;
}
}
}

vicentiu
6 Aug 2010, 5:14 AM
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!

Condor
6 Aug 2010, 5:26 AM
So you want:

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

vicentiu
7 Aug 2010, 6:30 AM
Thank you very much, Condor. That's what I wanted. I made a little modification and it works great.