如何使用jquery在表中附加JSON数据

时间:2017-05-10 15:54:30

标签: javascript jquery html json css3

我试图用jquery

读取这个json
{  
   "items":[  
      {  
         "srNo":1,
         "name":"first",
         "amount":1000
      },
      {  
         "srNo":2,
         "name":"second",
         "amount":1600
      }
   ],
   "fullname":"manav",
   "lastname":"kumar"
}

这是我到现在为止所尝试的

$.each(response,function(i,v){
    console.log(v.name);
});

如果可能请分享一些演示。 我想在表中附加项目并在上表中显示

3 个答案:

答案 0 :(得分:1)

尝试

import sys
import RPi.GPIO as GPIO
import time
import re
from PyQt4 import QtGui, uic, QtCore
import spidev

GPIO.setmode(GPIO.BCM) 
GPIO.setwarnings(False)

o2zero = 26
o2span = 19
cozero = 13
cospan = 6
co2zero = 5
co2span = 21

status = "nil"

o2_channel = 0
o2_temp_channel = 1
spi = spidev.SpiDev()
spi.open(0,0)

class MyWindow(QtGui.QMainWindow): 
def __init__(self):
    super(MyWindow, self).__init__()
    uic.loadUi('mainwindow.ui', self)

    self.o2_zero_up.clicked.connect(self.o2zeroup) 
    self.o2_zero_down.clicked.connect(self.o2zerodown)
    self.o2_span_up.clicked.connect(self.o2spanup)
    self.o2_span_down.clicked.connect(self.o2spandown)
    self.co_zero_up.clicked.connect(self.cozeroup)
    self.co_zero_down.clicked.connect(self.cozerodown)
    self.co_span_up.clicked.connect(self.cospanup)
    self.co_span_down.clicked.connect(self.cospandown)
    self.co2_zero_up.clicked.connect(self.co2zeroup)
    self.co2_zero_down.clicked.connect( self.co2zerodown)
    self.co2_span_up.clicked.connect(self.co2spanup)
    self.co2_span_down.clicked.connect(self.co2spandown)  
    self.close_button.clicked.connect(self.gpiocleanup)

    self.thread = O2_Channel()
    self.thread.o2_concentration.connect(self.onChangeValue)
    self.thread.start()

    self.show()

def onChangeValue(self, values):
    o2_volts = values


def o2zeroup(self):  
    GPIO.setup(o2zero, GPIO.OUT)   
    GPIO.output(o2zero, 1) 
def o2zerodown(self): 
    GPIO.setup(o2zero, GPIO.OUT) 
    GPIO.output(o2zero, 0) 

def o2spanup(self):  
    GPIO.setup(o2span, GPIO.OUT)  
    GPIO.output(o2span, 1) 
def o2spandown(self): 
    GPIO.setup(o2span, GPIO.OUT) 
    GPIO.output(o2span, 0) 

def cozeroup(self):  
    GPIO.setup(cozero, GPIO.OUT)  
    GPIO.output(cozero, 1) 
def cozerodown(self):
    GPIO.setup(cozero, GPIO.OUT)  
    GPIO.output(cozero, 0) 

def cospanup(self):
    GPIO.setup(cospan, GPIO.OUT)   
    GPIO.output(cospan, 1) 
def cospandown(self): 
    GPIO.setup(cospan, GPIO.OUT) 
    GPIO.output(cospan, 0)

def co2zeroup(self):
    GPIO.setup(co2zero, GPIO.OUT)   
    GPIO.output(co2zero, 1) 
def co2zerodown(self): 
    GPIO.setup(co2zero, GPIO.OUT) 
    GPIO.output(co2zero, 0)

def co2spanup(self):
    GPIO.setup(co2span, GPIO.OUT)   
    GPIO.output(co2span, 1) 
def co2spandown(self): 
    GPIO.setup(co2span, GPIO.OUT) 
    GPIO.output(co2span, 0)   

def gpiocleanup(self): 
    GPIO.cleanup() 

def closeEvent(self, event):
    self.thread.stop()
    self.thread.quit()
    self.thread.wait()
    self.thread.deleteLater() 
    print ("GPIO CleanUP")
    GPIO.cleanup() 
    event.accept()   

def ReadChannel(channel):
    adc = spi.xfer2([1,(8+channel)<<4,0])
    data = ((adc[1]&3) << 8) + adc[2]
    return data

def ConvertVolts(data, places):
    volts = (data * 3.3) / float(1023)
    volts = round(volts,places)
    return volts    

class O2_Channel(QtCore.QThread):

o2_concentration = QtCore.pyqtSignal(int)

def __init__(self):
    QtCore.QThread.__init__(self)
    self.mRunning = True

def run(self):
    while self.mRunning:

        o2_level = ReadChannel(o2_channel)
        o2_volts = ConvertVolts(o2_level,2)

        print("{}".format(o2_volts))
        self.o2_concentration.emit(o2_volts)
        delay = .2                         
        time.sleep(delay)

def stop(self):
    self.mRunning = False

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    window = MyWindow()
    sys.exit(app.exec_())

答案 1 :(得分:1)

要阅读name属性中的items字段,您只需执行以下操作:

$.each(response.items, function(i, v) {
    console.log(v.name);
});

假设您的回答如下:

&#13;
&#13;
var response = {
  "items": [{
    "srNo": 1,
    "name": "first",
    "amount": 1000
  }, {
    "srNo": 2,
    "name": "second",
    "amount": 1600
  }],
  "fullname": "manav",
  "lastname": "kumar"
}

$.each(response.items, function(i, v) {
  console.log(v.name);
});

// To access fullname & lastname you can use
console.log(response.fullname);
console.log(response.lastname);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;

答案 2 :(得分:1)

嗨,我认为这是ajax post response

在xDreamCoding回答之后,你可以得到items array

修改一点。

    var newresp = JSON.parse(response);
    //to get names
    console.log(newresp.fullname);
    console.log(newresp.lastname);
    //to get items array
   newresp.items.forEach((e,i) => {
   console.log(e.name)
});