gpt4 book ai didi

javascript - 如何创建多个 json 数组并在同一个函数中传递所有数组

转载 作者:行者123 更新时间:2023-12-01 05:45:06 26 4
gpt4 key购买 nike

我是 Json 新手,并且陷入了无法理解的简单语法

我有一个 json:

{"Full_Talktime":[
{"Talktime":"300", "Validity":"Lifetime","Price":"Rs 300", "Description":"200 full talktime"},
{"Talktime":"300", "Validity":"Lifetime","Price":"Rs 300", "Description":"200 full talktime"},
{"Talktime":"300", "Validity":"Lifetime","Price":"Rs 300", "Description":"200 full talktime"},
{"Talktime":"300", "Validity":"Lifetime","Price":"Rs 300", "Description":"200 full talktime"},
{"Talktime":"300", "Validity":"Lifetime","Price":"Rs 300", "Description":"200 full talktime"},
{"Talktime":"300", "Validity":"Lifetime","Price":"Rs 300", "Description":"200 full talktime"},
{"Talktime":"300", "Validity":"Lifetime","Price":"Rs 300", "Description":"200 full talktime"}
]}

和我的功能:

$(function() {
$.getJSON('data.json', function(data) {
$.each(data.Full_Talktime, function(index, plan) {


var tblPara = $('<p class="col1">').html("<b>Talktime</b><br>");
var tblValidity = $('<p class="col2">').html("<b>Validity</b><br>");
var tblPrice = $('<p class="col3">');
var tblDescription = $('<p class="row2">').html("<b>Description</b><br>");
var endline =$('<div class="end">');
tblPara.append('<div class="para">' + plan.Talktime + '</div>');
tblValidity.append('<div class="para">' + plan.Validity + '</div>');
tblPrice.append('<div class="col3para">' + plan.Price + '</div>');
tblDescription.append('<div class="para">' + plan.Description + '</div>');
console.log(plan);

$(".content").append(tblPara).append(tblValidity).append(tblPrice).append(tblDescription).append(endline);
});
});
});

我需要创建一个类似的 json,如下所示:

{"Special_Recharge":[
{"Talktime":"300", "Validity":"Lifetime","Price":"Rs 300", "Description":"200 full talktime"},
{"Talktime":"300", "Validity":"Lifetime","Price":"Rs 300", "Description":"200 full talktime"},
{"Talktime":"300", "Validity":"Lifetime","Price":"Rs 300", "Description":"200 full talktime"},
{"Talktime":"300", "Validity":"Lifetime","Price":"Rs 300", "Description":"200 full talktime"},
{"Talktime":"300", "Validity":"Lifetime","Price":"Rs 300", "Description":"200 full talktime"},
{"Talktime":"300", "Validity":"Lifetime","Price":"Rs 300", "Description":"200 full talktime"},
{"Talktime":"300", "Validity":"Lifetime","Price":"Rs 300", "Description":"200 full talktime"}
]}

并为其运行相同的函数。我需要有关 Json 语法的帮助并为两个数组运行相同的函数。

最佳答案

有两个 json header “Full_Talktime”和“Special_Recharge”,因此必须有一些东西可以识别它将是哪个数据( header ),您可能使用两个单独的 ajax 调用来获取它。

因此根据该条件首先读取json数据data.Special_Recharge/data.Full_Talktime,例如 var dataFromDB = data.Special_Recharge & var dataFromDB = data.Full_TalkTime

然后调用你的函数循环:

$.each(dataFromDB, 函数(索引, 计划) { ...

关于javascript - 如何创建多个 json 数组并在同一个函数中传递所有数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27500632/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com