简体   繁体   中英

How to get json values for a key using javascript or jquery or angularjs?

I have the following json:

var jsonobj = {
    "title" : "Testing",
    "settings" : {
    "mysettings" : false
    },
    "jsonlist": ["TestingList"],
    "testjsonvals": {
        "Test1": {
            "name": "name1",
            "description": "Test1 description"
            },
        "Test2": {
            "name": "name2",
            "description": "Test2 description"      
        },
        "Test3": {
            "name": "name3",
            "description": "Test3 description"            
        }
    }
}

How can I get/show description values of Test1, Test2, Test3 only from my given json on one alert message(like: Test1 description, Test2 description, Test3 description should show on my alert message) either using javascript or jquery or angularjs ? Please help me and Thanks in advance.

you should iterate jsonobj["testjsonvals"] (or jsonobj.testjsonvals ) object keys and concatenate description values, or push them into array and join()

 var jsonobj = { "title" : "Testing", "settings" : { "mysettings" : false }, "jsonlist": ["TestingList"], "testjsonvals": { "Test1": { "name": "name1", "description": "Test1 description" }, "Test2": { "name": "name2", "description": "Test2 description" }, "Test3": { "name": "name3", "description": "Test3 description" } } } var msg = []; $.each(jsonobj["testjsonvals"], function(key,v){ msg.push(v["description"]) }); alert(msg.join(", "));
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

you can do like this

var tests=jsonobj['testjsonvals'];
alert(tests["Test1"]['description'])
var message = jsonobj.testjsonvals.Test1.description + ', ' +
    jsonobj.testjsonvals.Test2.description + ', ' +
    jsonobj.testjsonvals.Test3.description;
alert(message);
alert( jsonobj ['testjsonvals'['Test1'].description,  + 
       jsonobj ['testjsonvals'] ['Test2'].description,  + 
       jsonobj ['testjsonvals']['Test3'].description );

or

alert( jsonobj.testjsonvals.Test1.description, + 
       jsonobj.testjsonvals.Test2.description,  + 
       jsonobj.testjsonvals.Test3.description );

Let me know if this code helped you

This will loop over all your testjsonvals and add them to an array that we join together with space and alert

var arr = [];
for(i in jsonobj.testjsonvals)
{
    arr.push(jsonobj.testjsonvals[i].description);
}

alert(arr.join(" "));

If you iterate through jsonobj.testjsonvals you should be able to build up your string to match what you require. Something like this should do the trick:

var jsonobj = {
    "title" : "Testing",
    "settings" : {
    "mysettings" : false
    },
    "jsonlist": ["TestingList"],
    "testjsonvals": {
        "Test1": {
            "name": "name1",
            "description": "Test1 description"
            },
        "Test2": {
            "name": "name2",
            "description": "Test2 description"      
        },
        "Test3": {
            "name": "name3",
            "description": "Test3 description"            
        }
    }
};

var tests = jsonobj.testjsonvals;
var msg = [];
for (var i in tests) {
  if (tests.hasOwnProperty(i)) {
    msg.push(tests[i].description);
  }
}
alert(msg.join(' '));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM