Javascript 对象对象对象数组Json.stringify字符串数组

Javascript 对象对象对象数组Json.stringify字符串数组,javascript,arrays,sha,Javascript,Arrays,Sha,我有一个对象数组,我在上使用了JSON.stringify(),现在我可以看到数组中有什么,但是当我执行arr[0]等操作时,它只输出一个字母 arr = {"hello":"yes","because":"no"} arr[0] =h 我希望它输出整个值,而不仅仅是第一个字母 我的代码 var clientContext = SP.ClientContext.get_current(); var peopleManager = new SP.UserPro

我有一个对象数组,我在上使用了
JSON.stringify()
,现在我可以看到数组中有什么,但是当我执行
arr[0]
等操作时,它只输出一个字母

arr = {"hello":"yes","because":"no"}

arr[0] =h
我希望它输出整个值,而不仅仅是第一个字母

我的代码

        var clientContext = SP.ClientContext.get_current();

        var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);

    // Get user properties for the target user.
    // To get the PersonProperties object for the current user, use the
    // getMyProperties method.
    MyProperties = peopleManager.getMyProperties();

    // Load the PersonProperties object and send the request.
    clientContext.load(MyProperties);
    clientContext.executeQueryAsync(getMyNewsChoicesSuccess, getMyNewsChoicesFail);
    },

    getMyNewsChoicesSuccess = function () {
        //get the news choice by actually fieldname
        var MyChoices = JSON.stringify(MyProperties.get_userProfileProperties().Value);
        $('#NBStest').text(MyChoices);

    },

它不再是数组,而是字符串。arr[0]将返回第一个字母


如果您想从中获取对象,您需要对其进行解析(尝试JSON.parse)

JSON.stringify()
完全按照它的声音执行。它将javascript对象转换为字符串。因此,当您执行
arr[0]
操作时,您将获得字符串中的第一个字母。如果您想获得实际值,需要将其转换回javascript对象。

您可以像这样从json字符串中获取第一个元素

JSON.parse(json_str)[0]
arr.hello = "yes";
// or
arr['hello'] = "yes";
但在您的示例中,第一个元素是“yes”,其索引是“hello”,这意味着您无法通过索引0获取第一个元素,但是可以通过其属性名获取它,如下所示

JSON.parse(json_str)[0]
arr.hello = "yes";
// or
arr['hello'] = "yes";
如果你想得到hello,这是关键,你必须使用这个循环

for (key in arr)
   console.log(key); 
// it will print 'hello' and then 'because'

如果
arr={“hello”:“yes”,“because”:“no”}
,则不会
JSON.stringify(arr)[0]
返回
“{”
?这也是我的猜测,但我不知道在他调用该方法后,运行了哪些其他代码来操作新字符串。“对象数组”是什么我认为如果你复制并粘贴你真正的代码会更清楚,因为你所写的
arr[0]
应该是
undefined
JSON。stringify(arr)[0]
应该是
”{
。你有一个术语问题。你的代码中没有数组。在JavaScript
var foo={“你好”:“是的”}定义一个对象。
称它为数组是不正确的。它没有数字索引(
0
);它只有命名属性(
“hello”
)。如果你想得到值
“yes”
你必须使用属性名,例如
foo.hello
foo[“hello”
(它们是等价的)。如果已使用
JSON.stringify
将对象转换为字符串,则
str[0]
将获得第一个(第0个)字符,即
{
。如果您想再次访问它的属性,您必须使用
JSON.parse将其转换回。如果这就是您调试的方式,我是否可以建议使用a。为什么当我放置arr[0]时,它返回h?parse只是将其转换回[object object],那么您希望显示什么值?