为什么我不能通过JavaScript中的键访问JSON对象?

时间:2023-01-27 16:05:23

This is my JSON object:

这是我的JSON对象:

{ text: 'stuff',
  user: 'user1
}

when I run a typeof jsonObj, I get object. When I run an jsonOb.length, I get undefined. When I tried to access the text property via console.log(jsonObj.text), I get undefined.

当我运行一个类型的jsonObj时,我得到了对象。当我运行jsonOb.length时,我得到了未定义。当我尝试通过console.log(jsonObj.text)访问text属性时,我得到了未定义。

So how can I properly access everything in JavaScript?

那么如何才能正确访问JavaScript中的所有内容?

I don't want to use jQuery as this is all node.js programming so it's serverside.

我不想使用jQuery,因为这是所有node.js编程所以它是服务器端。

UPDATED - full JSON

更新 - 完整的JSON

{ text: '@junk_666 おかえりか',
  user: 
   { display_name: 'mono',
     screen_name: 'monochrm',
     klout_score: null,
     location_str: '画面の前',
     utc_offset: '32400' },
  venue_id: 1304409836517,
  match_type: 'twitter',
  tweet_id: '116494264137023489',
  created_at_unix: 1316609371,
  meta: 
   { matchedPhrase: 'junk',
     venueName: 'deletemepls really long name' },
  tags: [ '' ],
  indexed_at_unix: 1316609416 }

4 个解决方案

#1


0  

Try this:

尝试这个:

{ text: 'stuff',
  user: 'user1'
}

You left off an apostrophe.

你没有撇号。

Now that you've posted your full JS code (that's not JSON, as @josnidhin points out)... works fine in jsFiddle. http://jsfiddle.net/Rs9R4/ I don't believe a JS Object has .length, just Arrays.

现在您已经发布了完整的JS代码(这不是JSON,正如@josnidhin指出的那样)...在jsFiddle中正常工作。 http://jsfiddle.net/Rs9R4/我不相信JS对象有.length,只有Arrays。

#2


1  

The json seems to be invalid

json似乎无效

{
    "text": "stuff",
    "user": "user1"
}

#3


1  

I copied and pasted your object into a FireBug console and it recognized it.

我将您的对象复制并粘贴到FireBug控制台中,然后识别出来。

If you need to count the number of key/value pairs, you can use a function such as this one to do it:

如果需要计算键/值对的数量,可以使用这样的函数来执行此操作:

function numMembers(o) {
    var i=0;
    for (a in o) {
        i++;
    }
    return i;
}

You should be able to access the value of the text property via jsonObj.text. Are you sure that your object is being referenced by jsonObj? Also, can you access the values for simpler objects such as the ones mentioned in other posts if you create them? Furthermore, does anything work if you use only ASCII characters? For some reason, it might not be handling some of the non-Latin characters properly.

您应该能够通过jsonObj.text访问text属性的值。你确定你的对象被jsonObj引用了吗?此外,您是否可以访问更简单对象的值,如创建它们时在其他帖子中提到的值?此外,如果您只使用ASCII字符,有什么用处吗?出于某种原因,它可能无法正确处理某些非拉丁字符。

#4


1  

First, what you have is not JSON because JSON requires property name to be in double quotes. It is a valid JavaScript object literal though, so I'll assume that's how you're using it.

首先,你拥有的不是JSON,因为JSON要求属性名称是双引号。它是一个有效的JavaScript对象文字,所以我假设你是如何使用它的。

Secondly, JavaScript objects in general do not have a length property. Arrays do.

其次,JavaScript对象通常没有length属性。数组做。

There's no problem with your object literal so there must be some other problem elsewhere in your code.

您的对象文字没有问题,因此代码中的其他地方一定存在其他问题。

#1


0  

Try this:

尝试这个:

{ text: 'stuff',
  user: 'user1'
}

You left off an apostrophe.

你没有撇号。

Now that you've posted your full JS code (that's not JSON, as @josnidhin points out)... works fine in jsFiddle. http://jsfiddle.net/Rs9R4/ I don't believe a JS Object has .length, just Arrays.

现在您已经发布了完整的JS代码(这不是JSON,正如@josnidhin指出的那样)...在jsFiddle中正常工作。 http://jsfiddle.net/Rs9R4/我不相信JS对象有.length,只有Arrays。

#2


1  

The json seems to be invalid

json似乎无效

{
    "text": "stuff",
    "user": "user1"
}

#3


1  

I copied and pasted your object into a FireBug console and it recognized it.

我将您的对象复制并粘贴到FireBug控制台中,然后识别出来。

If you need to count the number of key/value pairs, you can use a function such as this one to do it:

如果需要计算键/值对的数量,可以使用这样的函数来执行此操作:

function numMembers(o) {
    var i=0;
    for (a in o) {
        i++;
    }
    return i;
}

You should be able to access the value of the text property via jsonObj.text. Are you sure that your object is being referenced by jsonObj? Also, can you access the values for simpler objects such as the ones mentioned in other posts if you create them? Furthermore, does anything work if you use only ASCII characters? For some reason, it might not be handling some of the non-Latin characters properly.

您应该能够通过jsonObj.text访问text属性的值。你确定你的对象被jsonObj引用了吗?此外,您是否可以访问更简单对象的值,如创建它们时在其他帖子中提到的值?此外,如果您只使用ASCII字符,有什么用处吗?出于某种原因,它可能无法正确处理某些非拉丁字符。

#4


1  

First, what you have is not JSON because JSON requires property name to be in double quotes. It is a valid JavaScript object literal though, so I'll assume that's how you're using it.

首先,你拥有的不是JSON,因为JSON要求属性名称是双引号。它是一个有效的JavaScript对象文字,所以我假设你是如何使用它的。

Secondly, JavaScript objects in general do not have a length property. Arrays do.

其次,JavaScript对象通常没有length属性。数组做。

There's no problem with your object literal so there must be some other problem elsewhere in your code.

您的对象文字没有问题,因此代码中的其他地方一定存在其他问题。