1

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:

javascript object from parsed json

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!!

1
  • Do you do anything to the data after you parse it? Commented May 5, 2013 at 22:00

1 Answer 1

3

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).

Sign up to request clarification or add additional context in comments.

5 Comments

Alternatively, simply supply dataType: json incase the server isn't sending the headers, and it will be automatically parsed.
@Ohgodwhy: Yeah. Although really, the server should be supplying the correct content type.
a 100% agreement from my end; but just in case!
thanks! but this didn't make any difference. After some more tests it seemed that the values getting undefined only happened when I set async: false as a setting in the ajax call. However if I remove this the object isn't set properly and next steps in script flow will give an error. Any solution to this? Or is there a design pattern or something for these kind of situations?
@joskes: As you can see from the example in the answer, this does work. The problem you're having lies in code you haven't quoted. You'll need to create a small, self-contained example that replicates the behavior, and people will be able to help you see what's wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.