如何在json对象中只显示几个属性

如何在json对象中只显示几个属性

本文介绍了如何在json对象中只显示几个属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  {
    "items": [
      {
        "id": "engcall1",
        "action": {
          "action": "call",
          "dialcode": "9880214866",
          "msgbody": "",
          "url": ""
        }
      },
      {
        "id": "engsms1",
        "action": {
          "action": "sms",
          "dialcode": "9880214866",
          "msgbody": "Good morning",
          "url": ""
        }
      }]
}




if id is engcall1 then i should display only
"action": "call",
"dialcode": "9880214866"




function getvalue(id)
 {
 var data1;
 var data2;
 var data3;

var items = jsontxt.items;
         for (var i = 0; i < items.length; i++) {
               if (items[i].id == id ){  //search for id in items.
                var data= items[i].action;

				if(items[i].action.action == "call")
				{
				 data1= items[i].action.action;
				 data2= items[i].action.dialcode;

				}
					if(items[i].action.action == "sms")
				{
				 data1= items[i].action.action;
				 data2= items[i].action.dialcode;
				 data3= items[i].action.msgbody;

				}
				if(items[i].action.action == "ussd")
				{
			     data1= items[i].action.action;
				 data2= items[i].action.dialcode;

				}
				if(items[i].action.action == "url")
				{
				 data1= items[i].action.action;
				 data2= items[i].action.url;

				}

               }
		   }

		test(data1,data2,data3)
   }
  function test(data1,data2,data3){
		alert(data1);
		alert(data2);
		alert(data3);
	}	 	 




can this code optimized?? can it be done with any other logic??

推荐答案


这篇关于如何在json对象中只显示几个属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 17:38