简体   繁体   中英

How can I parse this JSON Object in javascript?

Looking for some guidance here on how can I extract Alok,Test and Test2

Object {value: "{"actors":["Alok","Test","Test2"]}"}

I have almost spent entire day to figure out the way to parse this JSON array but closest I reached is getting all characters printed in a single line.

My javascript implementation is:

        AJS.$.ajax({
            dataType: 'json',
            type: 'GET',
            url: AJS.params.baseURL+"/rest/leangearsrestresource/1.0/message/list/{actor}",
            async: false,
            multiple: true
        }).done(function(result) {
                    AJS.log(result);
                    //var obj = JSON.parse(result);
                    //AJS.log(obj.actors[1]);

            //AJS.log("Actor is " + result.length);
            var output = '';
            for(var key in result) {
                AJS.log("Key: " + key + " value: " + result[key]);
                var myThing = result[key];
                var output = myThing.property
                AJS.log(output)
               // AJS.log(output.text)
                for( thingKey in myThing ) {

                    AJS.log(myThing[thingKey])
                }

            }

Also attaching the console screenshot 在此处输入图片说明

结果对象未完全解析,它的属性值包含一个字符串,因此您必须像下面这样解析value属性的值:JSON.parse(result.value),您将获得一个具有一个“ actors”属性的对象value是一个包含字符串“ Alok”,“ Test”,“ Test2”的数组。

You're getting a json data that, when parsed, results in a object with a key named "value" and its pair value that is another json string.

So you have to get that string and parse it again.

Then you can access it as a JavaScript object.

This is the done callback rewritten:

function( result ) {
    var json,
        obj,
        actors;

    json = result.value;
    obj = JSON.parse( json );
    actors = obj.actors;

    for( idx in actors ) {
        AJS.log( actors[ idx ] );
    }
}  
for( thingKey in myThing.actors ) {

    AJS.log(myThing[thingKey])
}

You need to modify your code. Because the index is actors . actors is a array. So you can iterate array in for loop.

如果您确定自己具有有效的JSON,请使用以下代码:jQuery.parseJSON(str)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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