无法从服务器响应中获取数组值。

huangapple go评论166阅读模式
英文:

I cant get array value from my server responce

问题

I have translated the code portion for you:

  1. 我通过服务器收到一个字典响应
  2. console.log(responce) 返回以下内容
  3. {"filters":{
  4. "Facture": [
  5. "Магма (Тычок)",
  6. "Тонкий кирпич",
  7. "Гладкий",
  8. "Крафт",
  9. "Магма"
  10. ],
  11. "Color": [
  12. "Беж",
  13. "Черный",
  14. "Амстердам",
  15. "Лондон Брик",
  16. "Мюнхен",
  17. "Сити Брик"
  18. ],
  19. "Name": [
  20. "Облицованный кирпич стандартный пустотелый",
  21. "Тонкий колотый с ф. 180/11"
  22. ]
  23. }}
  24. 但是当我尝试使用以下代码访问值时
  25. console.log(xhr.responseText);
  26. const response = xhr.responseText;
  27. console.log(response);
  28. const myObj = JSON.parse(response);
  29. console.log(myObj);
  30. var names = myObj.Name.map(name => name.trim());
  31. var types = myObj.Facture.map(type => type.trim());
  32. var colors = myObj.Color.map(color => color.trim());
  33. console.log(names);
  34. console.log(types);
  35. console.log(colors);

如果在脚本内部创建此对象,它可以正常工作,但是当我通过响应获取对象时,它不起作用。我尝试了几乎所有方法,包括更改服务器发送的数据和使用不同的代码访问值,但仍然返回undefined。请帮助我获取值...

英文:

I have my server respond me with an dictionary.
The console.log(responce) returns this:

  1. {"filters":{
  2. "Facture": [
  3. "Магма (Тычок)",
  4. "Тонкий кирпич",
  5. "Гладкий",
  6. "Крафт",
  7. "Магма"
  8. ],
  9. "Color": [
  10. "Беж",
  11. "Черный",
  12. "Амстердам",
  13. "Лондон Брик",
  14. "Мюнхен",
  15. "Сити Брик"
  16. ],
  17. "Name": [
  18. "Облицованный кирпич стандартный пустотелый",
  19. "Тонкий колотый с ф. 180/11"
  20. ]
  21. }}

But when i try to access the value with this code:

  1. console.log(xhr.responseText);
  2. const response = xhr.responseText;
  3. console.log(response);
  4. const myObj = JSON.parse(response);
  5. console.log(myObj);
  6. var names = myObj.Name.map(name => name.trim());
  7. var types = myObj.Facture.map(type => type.trim());
  8. var colors = myObj.Color.map(color => color.trim());
  9. console.log(names);
  10. console.log(types);
  11. console.log(colors);

It returns undefined in the console. Please help me get the value....
The only clue that i got, that if i create this object inside of my script it works correct, but doesnt work when i get the object through the responce

I tried literally everything:
Changing the data i send by the server
Accessing the value with different code...

答案1

得分: 1

将这行代码更改为:

  1. const myObj = JSON.parse(response).filters;

因为你要查找的值似乎在属性filters内。

编辑

请注意,评论中提到服务器返回的是"{...}"而不是{...},因此我们不得不进行双重解析:

  1. const myObj = JSON.parse(JSON.parse(response)).filters;

如果你有服务器/后端的控制权,更好的解决方案是在那里进行修复。

英文:

Change this line:

  1. const myObj = JSON.parse(response);

to

  1. const myObj = JSON.parse(response).filters;

since the values you look for seem to be inside the property filters.

Edited

See, comments - it turns out the server returned "{...}" rather than {...} so we hade to double parse it:

  1. const myObj = JSON.parse(JSON.parse(response)).filters;

A better solution if you have control of the server/backend would be to fix this there.

huangapple
  • 本文由 发表于 2023年5月7日 05:36:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76191263.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定