无法循环通过JSON对象Javascript

时间:2022-11-29 20:56:11

I'm trying to loop through a JSON object using javascript. The reason that it is an object and not an array is because the same data is accessed using XAMARIN and C#'s NewtonJSON works better with just JSON Objects instead a mix of objects and arrays.

我正在尝试使用javascript循环访问JSON对象。它是一个对象而不是一个数组的原因是因为使用XAMARIN访问相同的数据,而C#的NewtonJSON只使用JSON对象而不是对象和数组的混合更好地工作。

Below is the JSON object that I am trying to work with, I've cut bits out so you can see the main part of the object that I'm having issues with.

下面是我正在尝试使用的JSON对象,我已经删除了一些内容,因此您可以看到我遇到问题的对象的主要部分。

CODE: NULL
VALUE:{
  USER24:{id: "24", business: "25", username: "test1", firstName: "test1", lastName: "test1", level: "0",…}
  USER25:{id: "25", business: "25", username: "test2", firstName: "test2", lastName: "test2", level: "0",…}
  USER26:{id: "26", business: "25", username: "test3", firstName: "test3", lastName: "test3", level: "0",…}
  USER27:{id: "27", business: "25", username: "test4", firstName: "test4", lastName: "test4",…}
  USER28:{id: "28", business: "25", username: "test5", firstName: "test5", lastName: "test5",…}
  USER29:{id: "29", business: "25", username: "test6", firstName: "test6", lastName: "test6", level: "0",…}
  USER30:{id: "30", business: "25", username: "test7", firstName: "test7", lastName: "test7", level: "0",…}
}
EXTRA: NULL

The issue I'm having is when I loop through data.VALUE I get the keys and values of USER24 instead of the keys and values of data.VALUE. When I try and loop through just data I get the keys CODE, VALUE, EXTRA like I would expect.

我遇到的问题是当我遍历data.VALUE时,我得到USER24的键和值,而不是data.VALUE的键和值。当我尝试循环访问数据时,我会得到像我期望的那样的密钥CODE,VALUE,EXTRA。

No matter what I place in the loop I won't get the keys and values USER24, USER25, USER26, ... which is what I want. Below I have snippet of the Javascript loop I'm using:

无论我在循环中放置什么,我都不会得到键和值USER24,USER25,USER26,......这就是我想要的。下面我有我正在使用的Javascript循环片段:

  for(var key in data.VALUE){
    if(data.VALUE.hasOwnProperty(key)){
      console.log('key:'+key+', val:'+data.VALUE[key]);
    }
  }

All I get back from this is:

我从这里得到的是:

key:id, val:24
key:business, val:25
key:username, val:test1
key:firstName, val:test1
key:lastName, val:test1
key:level, val:0
key:email, val:test1@test.co.uk
key:phone, val:null
key:isAdmin, val:true

Which is completely wrong!

这是完全错误的!

I'm not sure what else I am to do for this, I've tried using the $.each from JQuery, I've not used the new let[key, value] as it's far too new at the moment to rely on.

我不知道我还要为此做些什么,我已经尝试过使用JQuery的$ .each,我没有使用新的let [key,value],因为它现在太依旧了。

If you need anymore information or code snippets feel free to ask! Thanks in advanced for any help!

如果您需要更多信息或代码片段,请随时询问!感谢先进的任何帮助!

Here is a picture of the JSON in the preview tab in chrome (minus some sensitive data):

以下是chrome中预览选项卡中JSON的图片(减去一些敏感数据):

无法循环通过JSON对象Javascript

1 个解决方案

#1


0  

You can done it by $.each from JQuery like this

你可以通过JQuery这样的$ .each来完成它

$.each(data.VALUE, function (i, key) {
         $.each(key, function (j,k) {
              console.log('key:'+j+', val:'+k);
         });
     });

If you want to access it from javascript You are get each key in data.VALUE.USER24 object.

如果你想从javascript访问它你将获得data.VALUE.USER24对象中的每个键。

for (var key in data.VALUE.USER24) {
  if(data.VALUE.USER24.hasOwnProperty(key)){
  console.log('key:' + key + ', val:' + data.VALUE.USER24[key]);
  }
}

#1


0  

You can done it by $.each from JQuery like this

你可以通过JQuery这样的$ .each来完成它

$.each(data.VALUE, function (i, key) {
         $.each(key, function (j,k) {
              console.log('key:'+j+', val:'+k);
         });
     });

If you want to access it from javascript You are get each key in data.VALUE.USER24 object.

如果你想从javascript访问它你将获得data.VALUE.USER24对象中的每个键。

for (var key in data.VALUE.USER24) {
  if(data.VALUE.USER24.hasOwnProperty(key)){
  console.log('key:' + key + ', val:' + data.VALUE.USER24[key]);
  }
}