简体   繁体   English

如何将JSON对象转换为JavaScript中的数组

[英]How can I convert a json object to an array in javascript

Here is a snippet of javascript from my C# web MVC application: 这是我的C#Web MVC应用程序中的javascript代码段:

$.ajax({
         url: 'myurl'
        }).done(function(response) {
          $scope.Vdata = JSON.parse(response);
          return $scope.$apply();
        });

The JSON response form this call looks like this 此调用的JSON响应形式如下所示

"{
    \"renditions\": {
        \"live\": \"true\",
        \" type\": \"default\",
        \"rendition\": {
            \"name\": \"Live \",
            \"url\": \"http: //mysite\"
        }
    }
}"

I would like to wrap the json response rendition object into an array to look like this-(note the added square brackets for the array) 我想将json响应表示对象包装到一个数组中,看起来像这样-(请注意为该数组添加了方括号)

"{
    \"renditions\": {
        \"live\": \"true\",
        \" type\": \"default\",
        \"rendition\": [{
            \"name\": \"Live \",
            \"url\": \"http: //mysite\"
        }]
    }
}"

I tried something like this which didn't work: 我尝试了类似的方法,但是没有用:

$.ajax({
    url: 'myurl'
}).done(function(response) {
    var tmp;
    if (!respose.renditons.rendition.isArray) {
        tmp = respose.renditions.renditon;
        respose.renditon = [];
        respose.renditon.push(tmp);
    }
    $scope.Vdata = JSON.parse(response);
    return $scope.$apply();
});

The response will sometimes include the rendition object as an array so I only need to convert to an array in cases where its not. 响应有时会将演绎对象包含为数组,因此,我只需要在不包含数组的情况下将其转换为数组。

Can someone please help me with the correct javascript to convert the json object into an array. 有人可以帮助我使用正确的JavaScript将json对象转换为数组。 Preferably modifying my existing code 最好修改我现有的代码

You can check if the object is an array using this : 您可以检查如果对象是一个使用数组

Object.prototype.toString.call( response.renditions.rendition ) === '[object Array]'

And you can simplify the conversion to an array -- just wrap it as an array using x = [x] : 您可以简化到数组的转换-只需使用x = [x]将其包装为数组即可:

if (Object.prototype.toString.call( response.renditions.rendition ) !== '[object Array]') {
    response.renditions.rendition = [response.renditions.rendition];
}

Fiddle demo. 小提琴演示。

Try this: 尝试这个:

$.ajax({
    url: 'myurl'
}).done(function(response) {
    var json = JSON.parse(response);
    if(!Array.isArray(json.renditions.rendition)) {
        json.renditions.rendition = [json.renditions.rendition];
    }
    return json;
});

Fiddle demo (kind of...) 小提琴演示 (...的种类)

add data type JSON to your ajax post. 将数据类型JSON添加到您的ajax帖子中。 example

$.ajax({type: "POST",
        url: URL, 
        data: PARAMS,
        success: function(data){
            //json is changed to array because of datatype JSON
                alert(data.renditions);
            },            
        beforeSend: function (XMLHttpRequest) { 
            /* do something or leave empty */
        },
            complete: function (XMLHttpRequest, textStatus) { /*do something or leave empty */ },  
        dataType: "json"}
       );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM