javascript说JSON对象属性未定义,尽管不是 [英] javascript says JSON object property is undefined although it's not

查看:357
本文介绍了javascript说JSON对象属性未定义,尽管不是的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个json对象,我将其打印到屏幕上(使用alert()函数):

I have a json-object, which I print to the screen (using alert()-function):

alert(object);

这是结果:

然后我要将id的值打印到屏幕上:

Then I want to print the value of the id to the screen:

    alert(object["id"]); 

结果是这样的:

如您所见,键"id"的值不是(!!!)未定义.

As you can see, the value of key "id" is not(!!!) undefined.

这到底是怎么回事?!

推荐答案

看起来您的json对象不是一个真正的对象,它是一个json字符串.为了将其用作对象,您将需要使用反序列化功能,例如JSON.parse(obj).对于如何反序列化JSON字符串,许多框架都有自己的实现.
当您尝试对真实对象执行alert(obj)时,结果将是[object Object]或类似的东西

Looks like your json object is not really an object, it's a json string. in order to use it as an object you will need to use a deserialization function like JSON.parse(obj). Many frameworks have their own implementation to how to deserialize a JSON string.
When you try to do alert(obj) with a real object the result would be [object Object] or something like that

这篇关于javascript说JSON对象属性未定义,尽管不是的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆