json对象与对象数组

时间:2021-02-02 21:18:58

I'm querying a remote server and receiving a json reponse. The format of the reponse depends on the number of objects in the response. If there is a single object it looks similar to:

我正在查询远程服务器并收到json响应。响应的格式取决于响应中的对象数。如果有一个对象,它看起来类似于:

"results": {
  "meeting": {
    "location": "Location A",
    "time": "1378033200"
  }
}

But if there is more then one object in the response, I get an array of objects instead:

但是如果响应中有多个对象,我会得到一个对象数组:

"results": {
  "meeting": [
    {
      "location": "Location A",
      "time": "1378033200"
    },
    {
      "location": "Location B",
      "time": "1379250000"
    }
  ]
}

The complete response from the server includes a "count" variable, so I can distinguish between the two situations. In my Javascript at the moment I first check the count, and if there is exactly one object, I read out the location and time information similar to:

来自服务器的完整响应包括“计数”变量,因此我可以区分这两种情况。在我的Javascript中,我首先检查计数,如果只有一个对象,我会读出类似于的位置和时间信息:

var location = results.meeting.location;
var time = results.meeting.time;

If there is anything other than exactly one object, I do

如果除了一个对象之外还有其他东西,我会这样做

for(var i=0; i<count; i++) {
  var location = results.meeting[i].location;
  var time = results.meeting[i].time;
}

This works, but I'm wondering if there is a more elegant way of handling the two situations?

这有效,但我想知道是否有更优雅的方式来处理这两种情况?

3 个解决方案

#1


5  

Always you can loop it as an array,

总是可以将它作为数组循环,

if(!(results.meeting instanceof Array)){
   results.meeting = [results.meeting];
} 

So you will get always an array , if one an array with one data

因此,如果一个数组包含一个数据,您将始终获得一个数组

So and after always

所以以及永远

var count = results.meeting.length;
for(var i=0; i<count; i++) {
  var location = results.meeting[i].location;
  var time = results.meeting[i].time;
}

#2


0  

The best way to handle this situation is to change your JSON response so that meeting is always an array, even if it only has one entry. If you aren't able to update the way the JSON is being created, a simple way is to test using instanceof:

处理这种情况的最佳方法是更改​​JSON响应,以便会议始终是一个数组,即使它只有一个条目。如果您无法更新JSON的创建方式,一种简单的方法是使用instanceof进行测试:

if(results.meeting instanceof Array) {
    //array
} else {
   //object
}

#3


0  

You could do:

你可以这样做:

var meetings = result.count ? result.meeting : [result.meeting];

But the better overall design certainly is to always return an array in the first place.

但更好的整体设计当然是始终返回阵列。

#1


5  

Always you can loop it as an array,

总是可以将它作为数组循环,

if(!(results.meeting instanceof Array)){
   results.meeting = [results.meeting];
} 

So you will get always an array , if one an array with one data

因此,如果一个数组包含一个数据,您将始终获得一个数组

So and after always

所以以及永远

var count = results.meeting.length;
for(var i=0; i<count; i++) {
  var location = results.meeting[i].location;
  var time = results.meeting[i].time;
}

#2


0  

The best way to handle this situation is to change your JSON response so that meeting is always an array, even if it only has one entry. If you aren't able to update the way the JSON is being created, a simple way is to test using instanceof:

处理这种情况的最佳方法是更改​​JSON响应,以便会议始终是一个数组,即使它只有一个条目。如果您无法更新JSON的创建方式,一种简单的方法是使用instanceof进行测试:

if(results.meeting instanceof Array) {
    //array
} else {
   //object
}

#3


0  

You could do:

你可以这样做:

var meetings = result.count ? result.meeting : [result.meeting];

But the better overall design certainly is to always return an array in the first place.

但更好的整体设计当然是始终返回阵列。