Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
  {
    "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??
Posted
Updated 2-Jun-15 1:31am
v2

1 solution

alert (jsonObject.items[0].action.action);
alert (jsonObject.items[0].action.dialcode);

You'll probably want to loop foreach item in jsonObject.items rather than using index properties.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900