无法提取json响应

时间:2020-08-18 00:43:20

标签: json reactjs axios

我正在尝试提取JSON响应。当我尝试访问json数组中的对象时,它返回未定义的

import pandas as pd 
data = {
 'obligacion': [200000000123, 200000000123, 200000000123, 200000000123, 200000000123, 200000000123, 200000000123, 200000000123, 200000000123, 200000000123, 200000000123, 200000000123, 200000000123, 200000000123, 200000000123, 200000000123, 200000000123, 200000000123] +
               [200000000444, 200000000444, 200000000444, 200000000444, 200000000444, 200000000444, 200000000444, 200000000444, 200000000444, 200000000444, 200000000444, 200000000444, 200000000444, 200000000444, 200000000444, 200000000444, 200000000444, 200000000444],
 '0': [ 'mora18', 'mora17', 'mora16', 'mora15', 'mora14', 'mora13', 'mora12', 'mora11', 'mora10', 'mora9', 'mora8', 'mora7', 'mora6', 'mora5', 'mora4', 'mora3', 'mora2', 'mora1'] +
      [ 'kiwi18', 'kiwi17', 'kiwi16', 'kiwi15', 'kiwi14', 'kiwi13', 'kiwi12', 'kiwi11', 'kiwi10', 'kiwi9', 'kiwi8', 'kiwi7', 'kiwi6', 'kiwi5', 'kiwi4', 'kiwi3', 'kiwi2', 'kiwi1'], 
 'dias_mora': [ '-1', '-1', '-1', '-1', '-1', '-1', '-1', '4', '6', '0', '8', '9', '7', '10', '3', '2', '3', '2'] +
              [ '12', '0', '4', '4', '4', '7', '10', '4', '-6', '-7', '8', '8', '17', '10', '10', '-2', '3', '2']
}

df = pd.DataFrame.from_dict(data)  # convert dictionary to dataframe

dict_count = {}
for nid in df.obligacion.unique():
    vector_mora = df['dias_mora'][df.obligacion == nid].values
    groups = groupby(vector_mora)
    result = [(label, sum(1 for _ in group)) for label, group in groups] 
    dict_count[nid] = result
dict_count

2 个答案:

答案 0 :(得分:1)

您只需使用Array#mapArray#forEach函数即可获取所有JSON数据。您不需要在回复中使用JSON.stringify

演示:

let weather = [{
  "id": 711,
  "main": "Smoke",
  "description": "smoke",
  "icon": "50d"
}]

weather.map(function(x) {
  console.log(x.id) //711
  console.log(x.main) //Smoke
  console.log(x.description) //smoke
  console.log(x.icon) //50d
})

答案 1 :(得分:0)

我不确定您要完成什么或尝试过什么,但是这是如何工作的:

const weather = [{"id":711,"main":"Smoke","description":"smoke","icon":"50d"}]
document.querySelector('#id').textContent = weather[0].id
document.querySelector('#json').textContent = JSON.stringify(weather)
ID:
<div id="id"></div>
Stringified JSON:
<div id="json"></div>

要访问数组的元素,您可以引用索引或对其进行循环:

const myArray = [1, 2, 3];
myArray[0] // 1
myArray[1] // 2
myArray[2] // 3

for (let i = 0 ; i < myArray.length ; i++) {
    console.log(myArray[i]);
}

要访问对象的属性,可以使用点符号或键名作为索引:

const myObject = {'a': 1, 'b': 2}
myObject.a // 1
myObject['b'] // 2

JSON.stringify将JSON转换为字符串。这对于通过连接将其发送到可能识别或不能识别JSON的主机很有用。