0

I am having the JSon file that includes the following items:

{
"resource":"A",
"literals":["B","C","D"]
}

I would like to retrieve the items B, C and D only and store them in an array as strings. Here is my code:

<script>
// Reading the JSon file that has the items above
$.getJSON( "/Users/Docs/sample.json", function( data ) {

$.each( data, function( key, val ) {
    items.push( "<li id='" + key + "'>" + val + "</li>" );
});
</script>

Could anyone please help me to get only B, C and D and sore them in an array of strings so I can reuse them for later in another script? Your help would be very much appreciated.

4
  • data.literals will be your array of strings. Commented Nov 1, 2013 at 20:31
  • 1
    The items B, C and D are already stored as strings in an array, just use data.literals to get them Commented Nov 1, 2013 at 20:31
  • You replied with a piece of codes. I copied it and pasted. It is working correctly. Please add your codes that you have removed. That was what I needed. Thank you very much. Commented Nov 1, 2013 at 20:35
  • @user2864315 fair enough, if it helped. Commented Nov 1, 2013 at 20:40

1 Answer 1

1

Unless I've misunderstood the question, you need to access the array in the object by data.literals. Try this:

$.getJSON("/Users/Docs/sample.json", function(data) {    
    $.each(data.literals, function(i, val) {
        items.push("<li id='" + val + "'>" + val + "</li>" );
    });
});
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.