Firstly I will show you what I got:
My Json:
{"cols":[{"label":"Datum","type":"datetime"},{"label":"UsindsVorigeCall","type":"number"}],"rows":[{"c":[{"v":"Date(2010,02,20,05)"},{"v":"0.2300"}]},{"c": [{"v":"Date(2010,02,20,06)"},{"v":"0.0000"}]},{"c": [{"v":"Date(2010,02,20,07)"},{ "v":"0.0000"}]}]}
What I want to do is to change the value inside of my JSON and this is the solution I tried.
My code :
var jsonData = $.ajax({
url: 'output.json',
dataType: 'json',
data: 'UsindsVorigeCall',
crossDomain: true,
async: false,
success: function(data) {
var obj1,obj2;
json = $.each(data, function(key,value){
if(key==['rows']){
$.each(value, function(x,y){
$.each(y,function(prop,val){
$.each(val,function(a,b){
$.each(b,function(p,v){
str = JSON.stringify(b);
if(v>0){
v = 1;
}else if(v<=0){
v = 0;
}else{};
if(typeof v === 'string' ){
objs = '{"' + p + '":"' + v +'"}';
obj1 = JSON.parse(objs);
//console.log(obj1);
}else if(typeof v === 'number' ){
objn = '{"' + p + '":"' + v +'"}';
obj2 = JSON.parse(objn);
//console.log(obj2);
};
//console.log(obj1);
//console.log(obj2);
})
arr1 = [obj1];
arr2 = [obj2];
arr = $.merge(arr1,arr2);
console.log(arr);
})
})
})
}
})
}
})
If i am using obj1 or obj 2 inside of the if/else-statement it works as i want it to work (here you can see the console.log of obj1 and obj2):
Object {v: "Date(2010,02,20,05)"}
Object {v: "1"}
Object {v: "Date(2010,02,20,06)"}
Object {v: "0"}
Object {v: "Date(2010,02,20,07)"}
Object {v: "0"}
But outside of the if/else-statement the variables behave not as i want them to:
Object {v: "Date(2010,02,20,05)"}
undefined
Object {v: "Date(2010,02,20,05)"}
Object {v: "1"}
Object {v: "Date(2010,02,20,06)"}
Object {v: "1"}
Object {v: "Date(2010,02,20,06)"}
Object {v: "0"}
Object {v: "Date(2010,02,20,07)"}
Object {v: "0"}
Object {v: "Date(2010,02,20,07)"}
Object {v: "0"}
My question is :
Why do the variables behave like that?
How can I fix it so that the behave like I want them to?
Or is there maybe an easier way to change the value inside of the json?
Aucun commentaire:
Enregistrer un commentaire