zoukankan      html  css  js  c++  java
  • 封装json格式数据

    <div class="allPro">

    <div class="line-big">


    <div class="pro-item">
    <div class="media-img">
    <a href="/a/product/tiegao/2020/0529/33.html"><img src="/uploads/200529/1-200529110J4T1.jpg"></a>
    </div>
    <h3 class="text-center"><a href="/a/product/tiegao/2020/0529/33.html">颈肩腰腿痛冷敷贴</a></h3>
    </div>

    <div class="pro-item">
    <div class="media-img">
    <a href="/a/product/tiegao/2020/0529/39.html"><img src="/uploads/200529/1-20052911192W27.jpg"></a>
    </div>
    <h3 class="text-center"><a href="/a/product/tiegao/2020/0529/39.html">王氏秘贴 冷敷贴</a></h3>

    </div>


    </div>
    </div>

    js:

    var tota = $(".proAll .pro-item").length;

    var JsonData = {
    a: "",
    b: "",
    c: ""
    }
    var aa = [];

    for(var i = 0; i < tota; i++) {
    var JsonData = new Object();
    JsonData.a = $(".proImg").eq(i).attr("src");
    JsonData.b = $(".proTitle").eq(i).html();
    JsonData.c = $(".url").eq(i).attr("href");
    aa.push(JsonData);
    }

    var json = JSON.stringify(aa);

    console.log(json);

    前端调用的时候,要转换:

    var data = $.parseJSON(json);//

    $.parseJSON() 函数用于将符合标准格式的的JSON字符串转为与之对应的JavaScript对象。

  • 相关阅读:
    [HAOI2008]下落的圆盘
    10.2 上午 考试
    10.1 考试 ..........
    9.29 考试
    博弈论笔记
    bzoj_1022: [SHOI2008]小约翰的游戏John
    课程总结第十五周
    团队冲刺第二阶段09
    团队冲刺第二阶段08
    对搜狗输入法的评价
  • 原文地址:https://www.cnblogs.com/zyl-930826/p/13083588.html
Copyright © 2011-2022 走看看