0

I've been struggling with how to retrieve data from a JSON structured response to a query of a Sony Bravia TV. I've tried SEVERAL syntax variations to get just a single element's data, but no joy.

The response is sent to a textarea field having the id "log". Here is what gets displayed:

{
    "result": [
    [
        {
            "target": "speaker",
            "volume": 22,
            "mute": false,
            "maxVolume": 100,
            "minVolume": 0
        }
    ]
            ],
    "id": 1
}

The javascript function is below. In the last line, I'm trying to see a single element, but get nothing. What should the last line be?

    var gblJSONresponse = [];
    function send(service, method, params) {
        var ip = 'xxx.xxx.xxx.xxx';
        var psk = 'xxxx';
        var xhr = new XMLHttpRequest();
        xhr.onload = function() {
            var resp = xhr.responseText;
            gblJSONresponse = JSON.parse(xhr.response);
            log(JSON.stringify(JSON.parse(xhr.response), null, '  '));
        };

        xhr.open('POST', 'http://' + ip + '/sony/' + service);
        if (psk) {
            xhr.setRequestHeader('X-Auth-PSK', psk);
        }

        xhr.send(JSON.stringify({
            method: method,
            version: '1.0',
            id: 1,
            params: params ? [params] : [],
        }));

        window.alert("JSONresponse:\n"+JSONresponse[0].result[0].target);
    }

2 Answers 2

0

I think you are parsing response instead of responseText

Check responseType if set to json or text , if its set to text look into responseText otherwise it will appear in response

XMLHttpRequest.response Read only Returns an ArrayBuffer, a Blob, a Document, a JavaScript object, or a string, depending on the value of XMLHttpRequest.responseType, that contains the response entity body.

XMLHttpRequest.responseText Read only Returns a string that contains the response to the request as text, or null if the request was unsuccessful or has not yet been sent.

From Mozilla web docs here

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

1 Comment

responseType returns nothing. The displayed result in log looks like an object because of the parse, so I am attempting to access it as one. The same text is displayed in log if I replace .response with .responseText.
0

I've gotten it to work by placing the alert inside the xhr.onload function.

The line I placed in the function is: ... window.alert("JSONresponse:\n"+gblJSONresponse.result[0][0].target); ...

So, the answer was to treat the result of JSON.parse as an object/array. To access data from the 2nd dimension of the array, I had to use [0][0].

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

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.