0

I've got a JSON looking like this (design might be wrong)

{
  "Supermarkt": {
    "name": "Supermarkt",
    "translations": {
      "trolley": "Einkaufswagen",
      "vegetables": "Gemüse",
      "cocoa": "Kakao",
      "chocolate": "Schokolade"
    },
    "pronunciations": []
  },
  "Script1336Kidee": {
    "name": "Script1336Kidee",
    "translations": {
      "Trojaner": "RAT",
      "Laufzeit-Packer": "Magie",
      "PHP": "Der letzte Dreck",
      "JavaScript": "Wild-West"
    },
    "pronunciations": []
  }
}

Which I get with a ajax call (already JSON-decoded through dataType: "json" $.ajax option):

let lessonCall = $.ajax("https://www2.htw-dresden.de/~s70357/vokabel.php/",{dataType: "json"});
lessonCall.fail((jqXHR, status, error) => {
    console.log(status);
    console.log(error);
});
lessonCall.done((data,status) => {
    console.log(status);
    console.log(data);
    for (let lesson in data){
        console.log(lesson);
        console.log(lesson.name);
    }
});

Problem is lesson.name is undefined although console.log(data); show the healthy data Object with the healthy subsub-Objects, but lesson seems to be just a string-like thing.

How can I iterate through my "name"s?!

Superlokkus
  • 4,731
  • 1
  • 25
  • 57
  • `lesson` is the property name not the value, per [this](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in) – James Jun 07 '17 at 19:55
  • @James So how can I fix it? – Superlokkus Jun 07 '17 at 19:58
  • 1
    How do you get a property value from an object, having the property name in a variable? obj[propertyName]. – James Jun 07 '17 at 20:11
  • Yeah, I was not realizing that I'm operating at that "array-level" I was trying `lesson["name"]` but it never occurred to that for in just get's me the indexes. Javascript beginner, sorry, but thanks! – Superlokkus Jun 07 '17 at 20:16

2 Answers2

1

Have you tried calling let jsonData = JSON.parse(data) before your for loop?

Would you be able to modify the json format? If so, you could turn it into an array:

[   
  {
    "name": "Supermarkt",
    "translations": {
      "trolley": "Einkaufswagen",
      "vegetables": "Gemüse",
      "cocoa": "Kakao",
      "chocolate": "Schokolade"
    },
    "pronunciations": []   
  },   
  {
    "name": "Script1336Kidee",
    "translations": {
      "Trojaner": "RAT",
      "Laufzeit-Packer": "Magie",
      "PHP": "Der letzte Dreck",
      "JavaScript": "Wild-West"
    },
    "pronunciations": []   
  } 
]
Ervi B
  • 770
  • 7
  • 16
  • Yes, then I get parse erros as expected since it's already parsed by $.ajax : Uncaught SyntaxError: Unexpected token o in JSON at position 1 at JSON.parse () at Object.lessonCall.done (vokabeltrainer.js:31) at i (jquery.min.js:2) at Object.fireWith [as resolveWith] (jquery.min.js:2) at A (jquery.min.js:4) at XMLHttpRequest. – Superlokkus Jun 07 '17 at 19:56
  • What does the `console.log(lesson);` show? – Ervi B Jun 07 '17 at 19:59
  • Just `Supermarkt` and `Script1336Kidee` (just the property name not the value, as James pointed out) . Michał Perłakowski s Answer worked, I guess it's in one more array, I don't get that whole javascript "type system" yet. But thx for trying! – Superlokkus Jun 07 '17 at 20:03
  • 1
    Cool. No problem! I also posted another json format you could try if you have the access to modify it since you mentioned "(design might be wrong)" – Ervi B Jun 07 '17 at 20:07
  • @Eriv B Funny, I had it in this format, but IIRC that gives you parse errors, since it's not legal json. (That whole thing is top level) – Superlokkus Jun 07 '17 at 20:09
1

Try this:

for (let i in data){
    let lesson = data[i];
    console.log(lesson);
    console.log(lesson.name);
}
Michał Perłakowski
  • 88,409
  • 26
  • 156
  • 177