2017-02-20 13 views
0

Я пытаюсь проанализировать строку json с помощью jquery. Не получаю никакой ценности.Как разбирать строку json с помощью jquery

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() { 
     $('<li>' + data.2G.amount + '</li>').appendTo($amountlist); 
     $('<li>' + data.2G.detail + '</li>').appendTo($detaillist); 
     $('<li>' + data.2G.validity + '</li>').appendTo($validitylist); 
     $('<li>' + data.2G.talktime + '</li>').appendTo($talktimelist); 
    }); 
}); 

Here jsfiddle

+0

http://stackoverflow.com/questions/5071115/parse-json-string –

ответ

-1

Вы не можете получить доступ к 2G с dot. Вы можете использовать data["2G"], чтобы получить доступ к этому объекту.

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"}'; 

Также вы не итерация правильного изменения узла вы код, как этот

$.each(myData.data["2G"], function (i,data) { 
    alert(data.amount); 
    alert(data.detail); 
    alert(data.validity); 
    alert(data.talktime); 
}); 
+1

'2G' является действительным свойством. Но если вы хотите получить к нему доступ, вы должны написать 'data ['2G']'. –

+1

Но мы не можем получить к нему доступ, как 'data.2G', правильно? – Mairaj

+0

Вы не можете использовать точечную нотацию для доступа к '2G', но это не означает, что это не допустимое имя свойства, кроме того, вы можете получить к ней доступ, используя' data ['2G'] '. Ведущие номера отлично подходят для имен свойств, они просто недействительны в точечной нотации. –

1

Вы не перебор правильного узла. Попробуйте это:

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) { 
     $('<li>' + dataElem.amount + '</li>').appendTo($amountlist); 
     $('<li>' + dataElem.detail + '</li>').appendTo($detaillist); 
     $('<li>' + dataElem.validity + '</li>').appendTo($validitylist); 
     $('<li>' + dataElem.talktime + '</li>').appendTo($talktimelist); 
    }); 
}); 

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

+0

Мне нужен формат таблицы. можете ли вы сделать это как стол –

+0

Разве вы не слишком много просите? Это не ваш вопрос. Вы можете сделать это сами. –

0

я редактировать с defferent Например, пожалуйста, попытайтесь понять этот код.

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($('<p>').html('User: '+user.Name+'<br />Value: '+user.Value)); 
} 

Этот способ позволяет разобрать Наслаждайтесь.

0

Вы должны повторять это по myData.data [ '2G']

enter image description here

0

Вы не перебор должным образом. Измените его ниже фрагмент кода

$(document).ready(function() { 
    var loop = myData.data["2G"]; 
    for(var i = 0; i < loop.length; i ++) { 
     $("#amount").append('<li>' + loop[i].amount + '</li>'); 
     $("#detail").append('<li>' + loop[i].detail + '</li>'); 
     $("#validity").append('<li>' + loop[i].validity + '</li>'); 
     $("#talktime").append('<li>' + loop[i].talktime + '</li>'); 
    } 
}); 
0
$(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){ 

    $('<li>' + val.amount + '</li>').appendTo($amountlist); 
    $('<li>' + val.detail + '</li>').appendTo($detaillist); 
    $('<li>' + val.validity + '</li>').appendTo($validitylist); 
    $('<li>' + val.talktime + '</li>').appendTo($talktimelist); 
    }) 


}); 

});