JSON bilan kodni ajratish

Men kodni ishlayapman, lekin biroz muvaffaqiyatsiz: JSOnLint.com saytida tekshirganim kabi JSON javobim to'g'ri JSON.

Xullas, bu erda muammo tug'diradigan kodning qismi

$.post("items.cfm",{"term":request.term})
    .done(function(data){
        alert(data);
        try{
          var obj = $.parseJSON(data),
          values = [];
          alert(obj[x].id.split('~')[3]);

          for(var x = 0; x < obj.length; x++){
              values.push({
                "label":obj[x].text, "value":obj[x].id,
                "price":obj[x].id.split('~')[3]
              });
          }
          response(values);
        } catch(e) {
          alert(e);
        }
})

Agar men JSON-ni quyidagicha boshlasam:

{
    "text": "Blue CPR Pocket Mask",
    "id": "128177000003239005~Blue~YES~10.75"
}, {
    "text": "CPR Face Shields",
    "id": "128177000000138987~CPs~YES~41.83"
}

... Xatolik chiqaradi:

SyntaxError: kutilmagan ko'rsatkich, JSON-da

... lekin men boshlayman:

[{
        "text": "Blue CPR Pocket Mask",
        "id": "128177000003239005~Blue~YES~10.75"
    }, {
        "text": "CPR Face Shields",
        "id": "128177000000138987~CPs~YES~41.83"
    }
]

yaroqli JSON, lekin mening kodim bilan ajralib turadi, men uni ajratib olishga harakat qilaman va uning qiymatidan foydalanishni istayman, lekin u menga idning aniqlanmaganligini aytadi. shuning uchun uni ishlash uchun nima qilishim kerak.

# 1ni yangilash

Men har bir usulga rioya qilib, sinab ko'rdim va endi menga quyidagi xatolarni boshladi

TypeError: Cannot use 'in' operator to search for '4592' in 
0
console.log (obj) sizning qaytib kelishingizda nima ishlaydi?
qo'shib qo'ydi muallif A. Lau, manba
Sizda ikkita JSON moslamasi yo'q. Shunday qilib, ularni ikki xil uslubda ko'rib chiqing yoki ularni boshqa ob'ektning qatori yoki ichki ob'ektlarini birlashtiramiz. Bundan tashqari, nega tahlil qilishni xohlaysiz? Bu allaqachon JSON, shunday emasmi?
qo'shib qo'ydi muallif RaR, manba

6 javoblar

JSON noto'g'ri:

{},{};

Bu to'g'ri JSON:

[{},{}];

YoKI

{};
1
qo'shib qo'ydi

Kodingizda:

alert(obj[x].id.split('~')[3]);  

bu qator xatolarga olib keladi:

x is not defined  

ishni bajarish uchun ushbu qatorni for-loopga ko'chiring.

1
qo'shib qo'ydi

Muammo shundaki, obj o'zgaruvchining indeksi.

Shunday qilib, yechim:

$.post("items.cfm",{"term":request.term})
    .done(function(data){
        alert(data);
        try{
          var obj = $.parseJSON(data),
          values = [];
          //alert(obj[x].id.split('~')[3]); 
          //this guy is creating the issue.
          //since he's out of the loop where x is declared(out of scope)
          //Put it inside the loop and thins will work fine.

          for(var x = 0; x < obj.length; x++){
              alert(obj[x].id.split('~')[3]);  //Moved here(x is defined in the for loop)!
              values.push({
                "label":obj[x].text, "value":obj[x].id,
                "price":obj[x].id.split('~')[3]
              });
          }
          response(values);
        } catch(e) {
          alert(e);
        }
})

Umid qilamanki bu yordam.

0
qo'shib qo'ydi

Har bir döngüyü foydalanishga harakat qilib ko'ring, json agar [] bo'lsa, sizda qator bo'lishi kerak bo'lgan bir nechta ob'ekt bor

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

var data = [{
  "text": "Blue CPR Pocket Mask",
  "id": "128177000003239005~Blue~YES~10.75"
}, {
  "text": "CPR Face Shields",
  "id": "128177000000138987~CPs~YES~41.83"
}];
var values = [];
$.each(data, function(i, obj) {

  values.push({
    "label": obj.text,
    "value": obj.id,
    "price": obj.id.split('~')[3]
  });
});
console.log(values)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</div> </div>

To'g'ri json obyekti/satrini olganingizga ishonch hosil qiling

0
qo'shib qo'ydi
Xristian, kodni o'zgartirdim va endi bu TypeErrorni olaman: <4592 uchun qidirib topish uchun operatordan foydalana olmaysiz
qo'shib qo'ydi muallif Hane, manba

Buni bir qatorda ajratish kerak emas. Bundan tashqari, ogohlantirish funktsiyasidan foydalanadigan o'zgarmaydigan 'x' for loopning ustida. Shuning uchun siz "id" xatosi aniqlanmagan.

Bundan tashqari, bu men uchun ishladi.

var data = [{
            "text": "Blue CPR Pocket Mask",
            "id": "12817700000323900"
         },
        {
             "text": "CPR Face Shields",
        "id": "128177000000138987"
        }
        ]
        function test1(){
           var obj = data
            values = [];
            console.log(obj)

            for(var x = 0; x < obj.length; x++){
                values.push({"label":obj[x].text, "value":obj[x].id, "price":obj[x].id.split('~')[3]});
            }
        }
        test1()
0
qo'shib qo'ydi

ogohlantirishida (obj [x] .id.split ('~') [3]) aniqlanmagan. Uni biriktirgichga qo'ying, muammolarni hal qilishi mumkin:

  for(var x = 0; x < obj.length; x++){
      alert(obj[x].id);
      values.push({
        "label":obj[x].text, "value":obj[x].id,
        "price":obj[x].id.split('~')[3]
      });
  }
0
qo'shib qo'ydi
Javascript UZB
Javascript UZB
99 ishtirokchilar

@js_uzb @vuejs_uz @react_uz @nodejs_uz @angular_uz @ngTashkent @yiiframework_uz @laravel_uz @linux_uzbek @python_uz @swift_uzb —————— @uzdevgroup @UzGeeksGroup ——— @UzDev_Jobs @jobs_uzb