0

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 screenshotenter image description here

3
  • 4
    Either use dataType: 'json' in the request or result = JSON.parse( result ); in the callback. You currently just get a string and you should parse it before using it. Commented Jun 18, 2015 at 12:37
  • 1
    you need a JSON.parse(result), otherwise your 'result' is a string Commented Jun 18, 2015 at 12:37
  • 1
    If that is truly your JSON, the string is badly formed. Commented Jun 18, 2015 at 12:40

4 Answers 4

1

The result object is not fully parsed it has a property value which contains a string so you have to parse the value of value property like this: JSON.parse(result.value) and you will get an object with one property "actors" whose value is an array with the strings "Alok","Test","Test2".

Sign up to request clarification or add additional context in comments.

Comments

1

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 ] );
    }
}  

Comments

0
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.

Comments

-1

If you are sure that you have a valid JSON, use this: jQuery.parseJSON(str)

2 Comments

Thanks everyone for answering as the result object was not fully parsed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.