Home  >  Q&A  >  body text

javascript - How to loop through js or jquery to create a json format data?

'[{"imgPath": "C","price": "B","specDesc": "C"},{"imgPath": "D","price": "E","specDesc ": "F"}]' This is a parameter for an ajax request. How do I get the data on the page and create it in a loop to form a json? How should I do it? Please answer me! ! !


This is the page

仅有的幸福仅有的幸福2732 days ago547

reply all(4)I'll reply

  • 滿天的星座

    滿天的星座2017-05-19 10:45:06

    Press the control to take out the value, form an object literal, and then format it into JSON... To determine what kind of data is generated, you need to know how the interface is agreed upon.

    Making a pair should be simple, like

    var aModel = {
        imgPath: $("#imgPath").val(),
        price: $("#price").val()
    }

    Generating JSON is just one sentence

    var json = JSON.stringify(aModel)

    reply
    0
  • 黄舟

    黄舟2017-05-19 10:45:06

    JSON.parse()

    reply
    0
  • 仅有的幸福

    仅有的幸福2017-05-19 10:45:06

    var arr = [];
    var param = {}

    $("块").each(funciton(){
      var _temp = {"imgPath": "C","price": "B","specDesc": "C"};
         arr.push(_temp)
    
    })
    param.resutl=arr;

    reply
    0
  • ringa_lee

    ringa_lee2017-05-19 10:45:06

    You can use mockjs to simulate a lot of data.

    reply
    0
  • Cancelreply