从JSON中的对象数组中只获取一个元素

时间:2021-12-13 03:35:19

How can I get only the name from JSON file. Also code is perfectly working for getting the data from "file.json" i.e. that's not the problem for sure.

我怎样才能从JSON文件中获取名称。此外,代码完全适用于从“file.json”获取数据,即这不是问题肯定。

JavaScript:

var data = [];
function getName() {
  //what should I write here to get only name from the first object i.e. John
  //with this: data[0].name I am getting error!
}

var xhttp;
if(window.XMLHttpRequest)
  xhttp = new XMLHttpRequest();
else
  xhttp = new ActiveXObject("Microsoft.XMLHTTP");
    
xhttp.onreadystatechange = function() {
  if(xhttp.readyState == 4) {
    data = JSON.parse(xhttp.responseText);
    getName();
  }
}

xhttp.open("GET","file.json",true);
xhttp.send();

"file.json" - JSON:

“file.json” - JSON:

[
  {
    "name":"John",
    "city":"London"
  },
  {
    "name":"Maria",
    "city":"Rome"
  }
]

4 个解决方案

#1


6  

Pass the variable data through the function

通过函数传递变量数据

var data = [];
function getName(data) {
  return data[0].name;
}

var xhttp;
if(window.XMLHttpRequest)
  xhttp = new XMLHttpRequest();
else
  xhttp = new ActiveXObject("Microsoft.XMLHTTP");

xhttp.onreadystatechange = function() {
  if(xhttp.readyState == 4) {
    data = JSON.parse(xhttp.responseText);
    getName(data);
  }
}

xhttp.open("GET","file.json",true);
xhttp.send();

Also, if you want to retrieve all names, you can do something like this :

此外,如果要检索所有名称,可以执行以下操作:

function getName(data) {
  var names = [];
  for (var i = 0; i < data.length; i++) {
      names.push(data[i].name);
  }
  return names;
}

(The data is the array data)

(数据是数组数据)

#2


4  

Use Array.prototype.map() to transfrom items of your array:

使用Array.prototype.map()来转换数组的项目:

data.map(function(item) {
    return item.name
});

#3


1  

Your getName function should look like this:

你的getName函数应如下所示:

function getName(){
  a.forEach(function(i,j){
     console.log(i.name);
  });
}

#4


0  

It's worth mentioning that JSON datatypes are directly mapped to standard javascript datatypes. That means there's no such thing as JSON array, once you JSON.parse() a string, then you get plain javascript primitives: arrays, objects, strings, numbers, etc.

值得一提的是,JSON数据类型直接映射到标准的javascript数据类型。这意味着没有JSON数组这样的东西,一旦你JSON.parse()一个字符串,那么你得到简单的javascript原语:数组,对象,字符串,数字等。

So getting an element from a JSON array is just accessing an element of a standard javascript array. This is usually achieved by square brackets operator var element = array[index].

因此从JSON数组中获取元素只是访问标准javascript数组的元素。这通常通过方括号运算符var element = array [index]来实现。

Your code is however broken in several parts which are unrelated to array element access.

但是,您的代码在几个与数组元素访问无关的部分中被破坏。

First, you define the data variable at the toplevel - you just don't need it there (at least according to the sample).

首先,在顶层定义数据变量 - 您不需要它(至少根据样本)。

Then, you initialize it with an empty array - I have no idea why.

然后,用空数组初始化它 - 我不知道为什么。

Your onreadystatechange callback is simple when expressed in words: you get a string, parse it as json and then call getName supplying the result. That means your getName() should require a data argument which you then access within the function.

当用单词表示时,你的onreadystatechange回调很简单:你得到一个字符串,将其解析为json,然后调用getName提供结果。这意味着你的getName()应该需要一个数据参数,然后你可以在函数中访问它。

#1


6  

Pass the variable data through the function

通过函数传递变量数据

var data = [];
function getName(data) {
  return data[0].name;
}

var xhttp;
if(window.XMLHttpRequest)
  xhttp = new XMLHttpRequest();
else
  xhttp = new ActiveXObject("Microsoft.XMLHTTP");

xhttp.onreadystatechange = function() {
  if(xhttp.readyState == 4) {
    data = JSON.parse(xhttp.responseText);
    getName(data);
  }
}

xhttp.open("GET","file.json",true);
xhttp.send();

Also, if you want to retrieve all names, you can do something like this :

此外,如果要检索所有名称,可以执行以下操作:

function getName(data) {
  var names = [];
  for (var i = 0; i < data.length; i++) {
      names.push(data[i].name);
  }
  return names;
}

(The data is the array data)

(数据是数组数据)

#2


4  

Use Array.prototype.map() to transfrom items of your array:

使用Array.prototype.map()来转换数组的项目:

data.map(function(item) {
    return item.name
});

#3


1  

Your getName function should look like this:

你的getName函数应如下所示:

function getName(){
  a.forEach(function(i,j){
     console.log(i.name);
  });
}

#4


0  

It's worth mentioning that JSON datatypes are directly mapped to standard javascript datatypes. That means there's no such thing as JSON array, once you JSON.parse() a string, then you get plain javascript primitives: arrays, objects, strings, numbers, etc.

值得一提的是,JSON数据类型直接映射到标准的javascript数据类型。这意味着没有JSON数组这样的东西,一旦你JSON.parse()一个字符串,那么你得到简单的javascript原语:数组,对象,字符串,数字等。

So getting an element from a JSON array is just accessing an element of a standard javascript array. This is usually achieved by square brackets operator var element = array[index].

因此从JSON数组中获取元素只是访问标准javascript数组的元素。这通常通过方括号运算符var element = array [index]来实现。

Your code is however broken in several parts which are unrelated to array element access.

但是,您的代码在几个与数组元素访问无关的部分中被破坏。

First, you define the data variable at the toplevel - you just don't need it there (at least according to the sample).

首先,在顶层定义数据变量 - 您不需要它(至少根据样本)。

Then, you initialize it with an empty array - I have no idea why.

然后,用空数组初始化它 - 我不知道为什么。

Your onreadystatechange callback is simple when expressed in words: you get a string, parse it as json and then call getName supplying the result. That means your getName() should require a data argument which you then access within the function.

当用单词表示时,你的onreadystatechange回调很简单:你得到一个字符串,将其解析为json,然后调用getName提供结果。这意味着你的getName()应该需要一个数据参数,然后你可以在函数中访问它。