简体   繁体   English

如何从React JS(axios)中的json中提取一些数据?

[英]How to extract some data from json in react JS (axios)?

I'm a ReactJS and axios newbie. 我是ReactJS和axios新手。

I want to iterate and extract json data if the key is number (like 0, 1, 2....) and don't know how can I write this piece of code. 如果密钥是数字(例如0、1、2 ...),我想迭代并提取json数据,但不知道如何编写这段代码。 (because the server provide the json dynamically, and we don't know how many elements it will have) (因为服务器动态提供json,所以我们不知道它将有多少个元素)

Now I can extract the data with key = 0 (assume there exists this element) 现在,我可以使用键= 0提取数据(假设存在此元素)

class ContentBody extends Component {
    constructor(props) {
        super(props);
        this.state = {
            jdata: []
        }
    }
    componentDidMount() {
        var self = this;
        // read data periodically
        setInterval(function() {
            axios.get(URL)
                .then(function(response) {
                    self.setState({
                        jdata: response.data[0].Name
                    });
                })
                .catch(function(e) {
                    console.log("ERROR ", e);
                })
        }, 1000);
    }
}

// below is json data from the server
{
  "0": {
    "Assigned": false, 
    "Name": "BebopDrone-Test001.", 
    "droneID": 0
  }, 
  "1": {
    "Assigned": false, 
    "Name": "BebopDrone-B046836", 
    "droneID": 1
  }, 
  "2": {
    "Assigned": false, 
    "Name": "BebopDrone-Test002.", 
    "droneID": 2
  }, 
  "function": "getAllDroneStatus()"
}


// my pseudo code might be 
for(int i = 0; i < jsonObject.size(); i++){
    if(key is number){
        extract corresponding value
    }
 }

Your response is an Object not an Array. 您的响应是一个对象而不是一个数组。

You can't access an object using array indexing. 您不能使用数组索引访问对象。

Assuming response.data is the body of your json, you should access your object properties like this: response.data['0'] , response.data['1'] , response.data['2'] 假设response.data是json的主体,则应按以下方式访问对象属性: response.data ['0']response.data ['1']response.data ['2']

You can iterate over an object using for..in . 您可以使用for..in遍历对象。

Your data model (aside from the reference to 'getAllDroneStatus()') suggests that an array might be more useful. 您的数据模型(除了对“ getAllDroneStatus()”的引用之外)表明,数组可能更有用。

{
   "jdata" : [
      {
         "Assigned": false, 
         "Name": "BebopDrone-Test001.", 
         "droneID": 0
      }
    // ... 
    ]
}

Then you can iterate, reduce, filter and so on. 然后,您可以迭代,减少,过滤等。

The response that you get from the server is an object, what you should do is loop over the Object and then update data in state where I assume you only need name 您从服务器获得的响应是​​一个对象,您应该做的是遍历该对象,然后在我假设您只需要名称的状态下更新数据

componentDidMount() {
    var self = this;
    // read data periodically
    setInterval(function() {
        axios.get(URL)
            .then(function(response) {
                var names=[];
                Object.keys(response.data).forEach(function(data) {
                     names.push(data.Name);
                })
                self.setState({
                    jdata: names
                });
            })
            .catch(function(e) {
                console.log("ERROR ", e);
            })
    }, 1000);
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM