JSuery yordamida json satrini qanday ajratish mumkin

JSON-ni foydalanib, jsonni tekshirishga harakat qilaman. Hech qanday qadr-qimmatga ega emas.

var jsonString = '{"data":{"2G":[{"amount":"9","detail":"35 MB 2G Data , Post 35 MB you will be charged at 4p\/10kb","validity":"1 Day","talktime":"0"},{"amount":"16","detail":"90 MB 2G Data, Post 90 MB you will be charged at 4p\/10kb","validity":"2 Days","talktime":"0"},{"amount":"28","detail":"160 MB 2G Data, Post 160 MB you will be charged at 4p\/10kb","validity":"4 Days","talktime":"0"},{"amount":"54","detail":"300 MB 2G Data, Post 300 MB you will be charged at 4p\/10kb","validity":"7 Days","talktime":"0"},{"amount":"78","detail":"310 MB 2G Data , Post 310 MB you will be charged at 4p\/10kb","validity":"10 Days","talktime":"0"},{"amount":"95","detail":"550 MB 2G Data, Post 550 MB you will be charged at 4p\/10kb","validity":"14 Days","talktime":"0"},{"amount":"125","detail":"700 MB 2G Data, Post 700 MB you will be charged at 4p\/10kb","validity":"18 Days","talktime":"0"},{"amount":"155","detail":"850 MB 2G Data, Post 850 MB you will be charged at 4p\/10kb","validity":"21 Days","talktime":"0"},{"amount":"179","detail":"1 GB 2G Data, Post 1 GB you will be charged at 4p\/10kb","validity":"28 Days","talktime":"0"},{"amount":"198","detail":"Hero Recharge : Get 1.25 GB 2G Data assured benefit (upto 3 GB 2G Data with hero recharge)","validity":"28 Days","talktime":"0"},{"amount":"199","detail":"2 GB Unlimted 2G Data, Post 2 GB your speed will be reduced up to 40kbps","validity":"28 Days","talktime":"0"},{"amount":"249","detail":"3 GB Unlimted 2G Data, Post 3 GB your speed will be reduced up to 40kbps","validity":"28 Days","talktime":"0"},{"amount":"205","detail":"1 GB 2G Data Day + Extra 1 GB 2G Night Data (12AM to 6AM)","validity":"28 Days","talktime":"0"},{"amount":"305","detail":"2 GB 2G Data Day + Extra 2 GB 2G Night Data (12AM to 6AM)","validity":"28 Days","talktime":"0"},{"amount":"395","detail":"3 GB 2G Data Day + Extra 3 GB 2G Night Data (12AM to 6AM)","validity":"28 Days","talktime":"0"},{"amount":"57","detail":"190 MB 2G Data, Post 190 MB you will be charged at 4p\/10kb","validity":"28 Days","talktime":"0"},{"amount":"98","detail":"300 MB 2G Data, Post 300 MB you will be charged at 4p\/10kb","validity":"28 Days","talktime":"0"}]},"resCode":"200","resText":"SUCCESS"}';

var myData = JSON.parse(jsonString);

$(document).ready(function() {
    var $amountlist = $('#amount');
    var $detaillist = $('#detail');
    var $validitylist = $('#validity');
    var $talktimelist = $('#talktime');

    $.each(myData, function() {
        $('
  • ' + data.2G.amount + '
  • ').appendTo($amountlist);
            $('
  • ' + data.2G.detail + '
  • ').appendTo($detaillist);
            $('
  • ' + data.2G.validity + '
  • ').appendTo($validitylist);
            $('
  • ' + data.2G.talktime + '
  • ').appendTo($talktimelist);
        });
    });
    

    Bu erda jsfiddle

    0
    qo'shib qo'ydi muallif Harsha W, manba

    6 javoblar

    Siz to'g'ri tugunni yineleyemediniz. Buni ko'ring:

    var jsonString = '{"data":{"2G":[{"amount":"9","detail":"35 MB 2G Data , Post 35 MB you will be charged at 4p\/10kb","validity":"1 Day","talktime":"0"},{"amount":"16","detail":"90 MB 2G Data, Post 90 MB you will be charged at 4p\/10kb","validity":"2 Days","talktime":"0"},{"amount":"28","detail":"160 MB 2G Data, Post 160 MB you will be charged at 4p\/10kb","validity":"4 Days","talktime":"0"},{"amount":"54","detail":"300 MB 2G Data, Post 300 MB you will be charged at 4p\/10kb","validity":"7 Days","talktime":"0"},{"amount":"78","detail":"310 MB 2G Data , Post 310 MB you will be charged at 4p\/10kb","validity":"10 Days","talktime":"0"},{"amount":"95","detail":"550 MB 2G Data, Post 550 MB you will be charged at 4p\/10kb","validity":"14 Days","talktime":"0"},{"amount":"125","detail":"700 MB 2G Data, Post 700 MB you will be charged at 4p\/10kb","validity":"18 Days","talktime":"0"},{"amount":"155","detail":"850 MB 2G Data, Post 850 MB you will be charged at 4p\/10kb","validity":"21 Days","talktime":"0"},{"amount":"179","detail":"1 GB 2G Data, Post 1 GB you will be charged at 4p\/10kb","validity":"28 Days","talktime":"0"},{"amount":"198","detail":"Hero Recharge : Get 1.25 GB 2G Data assured benefit (upto 3 GB 2G Data with hero recharge)","validity":"28 Days","talktime":"0"},{"amount":"199","detail":"2 GB Unlimted 2G Data, Post 2 GB your speed will be reduced up to 40kbps","validity":"28 Days","talktime":"0"},{"amount":"249","detail":"3 GB Unlimted 2G Data, Post 3 GB your speed will be reduced up to 40kbps","validity":"28 Days","talktime":"0"},{"amount":"205","detail":"1 GB 2G Data Day + Extra 1 GB 2G Night Data (12AM to 6AM)","validity":"28 Days","talktime":"0"},{"amount":"305","detail":"2 GB 2G Data Day + Extra 2 GB 2G Night Data (12AM to 6AM)","validity":"28 Days","talktime":"0"},{"amount":"395","detail":"3 GB 2G Data Day + Extra 3 GB 2G Night Data (12AM to 6AM)","validity":"28 Days","talktime":"0"},{"amount":"57","detail":"190 MB 2G Data, Post 190 MB you will be charged at 4p\/10kb","validity":"28 Days","talktime":"0"},{"amount":"98","detail":"300 MB 2G Data, Post 300 MB you will be charged at 4p\/10kb","validity":"28 Days","talktime":"0"}]},"resCode":"200","resText":"SUCCESS"}';
    
    $(document).ready(function() {
        var $amountlist = $('#amount');
        var $detaillist = $('#detail');
        var $validitylist = $('#validity');
        var $talktimelist = $('#talktime');
        var myData = $.parseJSON(jsonString);    
        console.log(myData.data["2G"]);
        $.each(myData.data["2G"], function(i,dataElem) {
            $('
  • ' + dataElem.amount + '
  • ').appendTo($amountlist);
            $('
  • ' + dataElem.detail + '
  • ').appendTo($detaillist);
            $('
  • ' + dataElem.validity + '
  • ').appendTo($validitylist);
            $('
  • ' + dataElem.talktime + '
  • ').appendTo($talktimelist);
        });
    });
    

    http://jsfiddle.net/NJMyD/5183/

    1
    qo'shib qo'ydi
    Bir oz ko'proq so'ramaysizmi? Bu sizning savolingiz emas. Buni o'zingiz qilishingiz mumkin.
    qo'shib qo'ydi muallif Taha Paksu, manba
    Jadval formatini xohlayman. uni stol qilib olasizmi?
    qo'shib qo'ydi muallif Venkatesh Panabaka, manba

    nuqta </​​code> bilan 2G ga kira olmaysiz. Ushbu xususiyatga kirish uchun ma'lumotlar ["2G"] dan foydalanishingiz mumkin.

    var jsonString = '{"data":{"2G":[{"amount":"9","detail":"35 MB 2G Data , Post 35 MB you will be charged at 4p\/10kb","validity":"1 Day","talktime":"0"},{"amount":"16","detail":"90 MB 2G Data, Post 90 MB you will be charged at 4p\/10kb","validity":"2 Days","talktime":"0"},{"amount":"28","detail":"160 MB 2G Data, Post 160 MB you will be charged at 4p\/10kb","validity":"4 Days","talktime":"0"},{"amount":"54","detail":"300 MB 2G Data, Post 300 MB you will be charged at 4p\/10kb","validity":"7 Days","talktime":"0"},{"amount":"78","detail":"310 MB 2G Data , Post 310 MB you will be charged at 4p\/10kb","validity":"10 Days","talktime":"0"},{"amount":"95","detail":"550 MB 2G Data, Post 550 MB you will be charged at 4p\/10kb","validity":"14 Days","talktime":"0"},{"amount":"125","detail":"700 MB 2G Data, Post 700 MB you will be charged at 4p\/10kb","validity":"18 Days","talktime":"0"},{"amount":"155","detail":"850 MB 2G Data, Post 850 MB you will be charged at 4p\/10kb","validity":"21 Days","talktime":"0"},{"amount":"179","detail":"1 GB 2G Data, Post 1 GB you will be charged at 4p\/10kb","validity":"28 Days","talktime":"0"},{"amount":"198","detail":"Hero Recharge : Get 1.25 GB 2G Data assured benefit (upto 3 GB 2G Data with hero recharge)","validity":"28 Days","talktime":"0"},{"amount":"199","detail":"2 GB Unlimted 2G Data, Post 2 GB your speed will be reduced up to 40kbps","validity":"28 Days","talktime":"0"},{"amount":"249","detail":"3 GB Unlimted 2G Data, Post 3 GB your speed will be reduced up to 40kbps","validity":"28 Days","talktime":"0"},{"amount":"205","detail":"1 GB 2G Data Day + Extra 1 GB 2G Night Data (12AM to 6AM)","validity":"28 Days","talktime":"0"},{"amount":"305","detail":"2 GB 2G Data Day + Extra 2 GB 2G Night Data (12AM to 6AM)","validity":"28 Days","talktime":"0"},{"amount":"395","detail":"3 GB 2G Data Day + Extra 3 GB 2G Night Data (12AM to 6AM)","validity":"28 Days","talktime":"0"},{"amount":"57","detail":"190 MB 2G Data, Post 190 MB you will be charged at 4p\/10kb","validity":"28 Days","talktime":"0"},{"amount":"98","detail":"300 MB 2G Data, Post 300 MB you will be charged at 4p\/10kb","validity":"28 Days","talktime":"0"}]},"resCode":"200","resText":"SUCCESS"}';
    

    Bundan tashqari siz bunday kodni to'g'ri tugunni o'zgartirgandan ko'ra, yechilmaysiz

    $.each(myData.data["2G"], function (i,data) {
        alert(data.amount);
        alert(data.detail);
        alert(data.validity);
        alert(data.talktime);
    });
    
    1
    qo'shib qo'ydi
    2G tegishli xususiyatdir. Agar siz unga kirishni istasangiz, data ['2G'] ni yozishingiz kerak.
    qo'shib qo'ydi muallif t.niese, manba
    2G ga kirish uchun nuqta belgilaridan foydalana olmaysiz, lekin bu uning tegishli bo'lgan mulk nomi emasligini anglatmaydi, bundan tashqari data ['2G'] . Etakchi raqamlar mulk nomlari uchun juda yaxshi, ular nuqta belgisida faqat yaroqsiz.
    qo'shib qo'ydi muallif t.niese, manba
    Lekin biz buni data.2G ga o'xshamaysizmi?
    qo'shib qo'ydi muallif Mairaj, manba

    Siz uni qayta ishlashingiz kerak myData.data ['2G']

    enter image description here

    0
    qo'shib qo'ydi

    Siz to'g'ri javob bermadingiz. Uni quyidagi kod qismiga o'zgartiring

    $(document).ready(function() {
       var loop = myData.data["2G"];
       for(var i = 0; i < loop.length; i ++) {
          $("#amount").append('
  • ' + loop[i].amount + '
  • ');
          $("#detail").append('
  • ' + loop[i].detail + '
  • ');
          $("#validity").append('
  • ' + loop[i].validity + '
  • ');
          $("#talktime").append('
  • ' + loop[i].talktime + '
  • ');
       }
    });
    
    0
    qo'shib qo'ydi
    $(document).ready(function() {
    var $amountlist = $('#amount');
    var $detaillist = $('#detail');
    var $validitylist = $('#validity');
    var $talktimelist = $('#talktime');
    
    $.each(myData.data, function(key,value) {
            $.each(value,function(i,val){
    
       $('
  • ' + val.amount + '
  • ').appendTo($amountlist);
        $('
  • ' + val.detail + '
  • ').appendTo($detaillist);
        $('
  • ' + val.validity + '
  • ').appendTo($validitylist);
        $('
  • ' + val.talktime + '
  • ').appendTo($talktimelist);
        })
    
    
    });
    

    });

    0
    qo'shib qo'ydi

    Iltimos, ushbu kodni tushunishga harakat qilib ko'ring.

    var json = '{"Users":[{"Name":"user999","Value":"test"},{"Name":"test2","Value":"test"}]}';
    
    var json_parsed = $.parseJSON(json);
    
    for (var u = 0; u < json_parsed.Users.length; u++){
        var user = json_parsed.Users[u];
        $('body').append($('
    ').html('User: '+user.Name+'
    Value: '+user.Value)); }

    Shunday qilib, siz rohatlanasiz.

    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