Reputation: 61
I have a weird problem with parsing a JSON response coming from php source (symfony2). This is probably something very trivial but I'm not very skilled in javascript so have lost many hours on this already.
I have a serialized php-array in my db, which I unserialize and then convert to JSON.
$response->setContent(json_encode(unserialize($onderdeel->getArticles())));
On the client I just use jQuery to parse the json data.
$.ajax({
......
success: function(data){
articleObject = jQuery.parseJSON(data);
}
});
However this gives me some weird results, some of the values are set to undefined while they should have a value. However some of the values are ok.
This is the raw result I get from the php script before it get's parsed:
{
"onderdeel":{
"onderdeel_id":"1546",
"onderdeel_type":"overgordijnen160",
"onderdeel_naam":"",
"onderdeel_opmerkingen":"",
"berekend_prijs":"0",
"status":"",
"active_artikel_id":"0",
"naam_ruimte":"",
"opmerkingen":""
},
"artikels":[
{
"ruimte":"",
"opmerkingen":"",
"korting":"",
"berekend_aantal_banen":"2",
"aantal_banen_zelf_ingegeven":"",
"berekend_hoeveelheid":"400",
"berekend_multiplicator":"1.9",
"berekend_valide":"",
"berekend_prijs_met_korting":"0.00",
"berekend_prijs":"20040040.00",
"stap2":{
"valide":"valide",
"hoogte":"100",
"breedte":"100",
"banen":"stel",
"stof":{
"id":"9",
"naam":"AGRA",
"modelnummer":"123456",
"stofbreedte":"140.00",
"rapporthoogte":"100.00",
"kleur":"nul",
"prijspermeter":"100.00",
"wasvoorschriften":"COOL WASH COOL IRON",
"stock":" "
},
"railtype":{
"id":"7",
"naam":"rails type 1",
"modelnummer":"RT-2",
"stock":"200.00 stuks",
"rapporthoogte":"null",
"prijspermeter":"null",
"wasvoorschriften":"null"
}
},
"maakwijze":{
"status":"",
"maakwijze_type":"lint",
"plooi":"",
"retour_plooi":"",
"cm_plooi":"",
"hoofdje":"100",
"berekende_string":"LINT > gewone voering",
"voering_string":"gewone voering",
"voering":{
"voering_id":"",
"voering_prijs":"",
"voering_onderdeel":"",
"voering_type":""
},
"voering_aan":"true",
"confectie":{
"confectie_id":"2",
"confectie_prijs":"10000000.00",
"confectie_zoom":"25.31",
"confectie_onderdeel":"OG < 160",
"confectie_type":"LINT > gewone voering"
},
"valide":"valide",
"loodjes":"loodjes"
},
"prijs":{
"prijs_valide":"",
"prijs_korting":"",
"prijs_plaatsing":"",
"prijs_berekend_voor_artikel":"",
"prijs_berekend_voor_artikel_met_korting":"",
"prijs_berekend_stofprijs":"40000",
"prijs_berekend_confectieprijs":"20000000",
"prijs_berekend_prijslood":"40",
"prijs_berekend_voering":"0",
"prijs_railtype_prijs":""
}
}
],
"onderdeel_naam":"",
"onderdeel_opmerkingen":""
}
However after I parse it this is the result:
For example artikels.0.maakwijze.maakwijze_type is set to undefined while in the raw json it is set to 'lint'.
The weird thing is that if I just copy the raw json to the chrome console and parse it with the same function jQuery.parseJSON('copied text') all values are ok
I also replaced the jQuery.parseJSON with the standard JSON.parse , but this gave me the same result
Any ideas what causes this?
Thanks!!
Upvotes: 1
Views: 106
Reputation: 1075219
On the client I just use jQuery to parse the json data.
$.ajax({ ...... success: function(data){ articleObject = jQuery.parseJSON(data);
If your server is returning Content-Type: application/json
, data
will already be a parsed object. You don't want to parse it again.
Without the jQuery.parseJSON(data)
, it works for me (source).
Upvotes: 3