-5

I need to fetch the values from this JSON in my java script:

[{
        "selectionName": "Select",
        "subSelections": [{
                "id": 4,
                "subSelectionName": "Select",
                "description": "Deepmala"
            }
        ]
    }, {
        "selectionName": "week14",
        "subSelections": [{
                "id": 7,
                "subSelectionName": "1",
                "description": ""
            }
        ]
    }, {
        "selectionName": "test",
        "subSelections": [{
                "id": 6,
                "subSelectionName": "test",
                "description": ""
            }
        ]
    }, {
        "selectionName": "select",
        "subSelections": [{
                "id": 3,
                "subSelectionName": "sub-select",
                "description": "Created by Prakash"
            }
        ]
    }, {
        "selectionName": "testcreate",
        "subSelections": [{
                "id": 1,
                "subSelectionName": "testcreate",
                "description": ""
            }
        ]
    }, {
        "selectionName": "by htmlwidget",
        "subSelections": [{
                "id": 5,
                "subSelectionName": "by htmlwidget",
                "description": "created by html widget"
            }
        ]
    }
]

Any suggestions?

Christoph
  • 50,121
  • 21
  • 99
  • 128
Deepmala
  • 1
  • 1

2 Answers2

1

You could use something like JSONSelect to extract certain values.

http://jsonselect.org/

Here's an example of how to use it:

(found in this JSFiddle)

$(function(){
/*
Json as easy as SQL ??? RT @lloydhilaiel JSONSelect - CSS-like selectors for JSON - http://jsonselect.org
Testing...
*/
var jsonData = {
    "name": {
        "first": "Lloyd",
        "last": "Hilaiel"
    },
    "favoriteColor": "yellow",
    "languagesSpoken": [
        {
        "language": "Bulgarian",
        "level": 2},
    {
        "language": "English",
        "level": 1},
    {
        "language": "Spanish",
        "level": 7}
    ]
};

var selector = '.name > *'; // xPath CSS like selector

try {

    var resultObj = JSONSelect.match(selector, jsonData);
    console.log(typeof resultObj);
    console.log(resultObj);
    console.log('- - - - -');

    JSONSelect.forEach(selector, jsonData, function(resultObj) {
        console.log(typeof resultObj);
        console.log(resultObj);
        console.log('- - - - -');
        $('body').append('<p>' + $.trim(JSON.stringify(resultObj, null, ' ')) + '</p>');
    });

} catch(e) { console.log(e); }

});
davidhiggins
  • 99
  • 1
  • 5
  • the json I am geting is from jsp function getSelection() { var options = ""; $.getJSON('http://localhost:8080/r3/selection.jsp').done(function(json) { // var data ='http:../r3/selection.jsp'; alert(json.selectionName); // alert(json.subSelections); value.selectionName + ''; $.each(json.subSelections, function(index, value) { options += ''; }); }).fail(function (jqxhr, textStatus, error) { alert(' fail : '+error); }); } – Deepmala Apr 22 '13 at 18:27
0

JSON objects are easy to handle

var JSON = //Your JSON Object

JSON[0].selectName //returns 'Select'

JSON[0].subSelections[0].id //returns 4

and so on.

Any array objects can be treated as arrays. Any mapped objects can be returned by using the key like a field name for the JSON object.

Tamil Selvan C
  • 19,913
  • 12
  • 49
  • 70
ValSmith
  • 118
  • 7