0

I'm working on accessing elements inside a JSON string, but I'm receiving an error of

jQuery.Deferred exception: Object.keys(...).Each is not a function success@http....

Here is my code:

response =JSON.stringify('[{"file": "app.dll", "fields": {"name": "Misc App", "rank": 1}}]');
response = JSON.parse(response);
console.log(response);

response.forEach(function(element){
    console.log(element);
});

Bonus points for demonstrating how to display the names and values of the elements in the array!

2
  • Retagging to JS because there is no jQuery in the question Commented May 3, 2020 at 15:51
  • You stringify a string. Guess what is the result datatype ? ;) Commented May 3, 2020 at 15:51

1 Answer 1

1

Many things can be serialized in Javascript, including strings. For example, if you stringify:

const foo = 'foo';
JSON.stringify(foo);

you get

"foo"

Here, because you're stringifying a string the same way:

response =JSON.stringify('[{"file": "app.dll", "fields": {"name": "Misc App", "rank": 1}}]');

When you parse it later, you get back the original string, as intended.

Either don't stringify it at all initially:

let response = '[{"file": "app.dll", "fields": {"name": "Misc App", "rank": 1}}]';
response = JSON.parse(response);
console.log(response);

response.forEach(function(element){
    console.log(element);
});

Or, if you really have to, parse it twice:

let response = JSON.stringify('[{"file": "app.dll", "fields": {"name": "Misc App", "rank": 1}}]');
response = JSON.parse(JSON.parse(response));
console.log(response);

response.forEach(function(element){
    console.log(element);
});

(but that's a very weird solution - it'd be better by far not to stringify the string in the first place, since it's already in parseable JSON format)

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

Comments

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.