如何解码json-ajax响应

how to decode the json ajax response

本文关键字:json-ajax 响应 解码 何解码      更新时间:2023-09-26

我在解码ajax响应时遇到了问题,在这里我发送组织、位置和建筑作为输入,作为回报,它提供了2个键作为入口/出口和键,现在我的ajax调用工作正常,我可以提醒响应,现在我的要求是解码我得到的json数组,然后将entry/exit的值带到表单中一个名为entry/exist的表单字段中,并在表单中键入type字段。我曾尝试在php噬菌体中解码json,该噬菌体在调用ajax时执行,并将2个值存储到会话中,但当我给出值=$_SESSIN[type]时,它没有显示在表单字段中和$_SESSION[entry/exit]之后,我尝试使用控制台用javascript解码json脚本,任何人都可以找出我做错了什么。到此为止的代码是

 //ajax
 function ajax()
{
var org=document.getElementById('category_id').value;
alert(org);
var loc=document.getElementById('category_id1').value;
alert(loc);
var bui=document.getElementById('category_id2').value;   
alert(bui);
var req;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
   req=new XMLHttpRequest();
}
else
{// code for IE6, IE5
   req=new ActiveXObject("Microsoft.XMLHTTP");
}
req.open("POST", "ajax.php?&org="+org+"&loc="+loc+"&bui="+bui+"", true);
req.send();
req.onreadystatechange=function(){
   if(req.readyState==4&&req.status==200){
       //$(".error").hide();
       result=req.responseText
       alert(result);
       var strJSON = 'result';
       var objJSON = eval("(function(){return " + strJSON + ";})()");
       alert(objJSON.name);
       alert(objJSON.type);
       }
   }
}
 <form name="theForm" method="post" action="addmachine.php" enctype="multipart/form-data" onSubmit="return validate();">
      <label for="orgname">Organisation Name</label>
                <select style="width: 305px;text-align:left ;"  name="category_id" id="category_id" onchange="OrganisationName(this);">
                <option value="">Select</option>
                 <option value="1">1</option>
                 <option value="2">2</option>
                                  </select>
                <p>
    <label name="location">Location</label>
                 <select style="width: 305px;" name="category_id1" id="category_id1" onchange="LocationName(this);" >
                 <option value="">Select</option>
                 <option value="1">1</option>
                 <option value="2">2</option>
                 </select>
                </p>
                <p>
    <label for="building">Building</label>
                <select style="width: 305px" name="category_id2" id="category_id2" onchange="BuildingName(this);" onchange="ajax(this);">
                <option value="">Select</option>
                <option value="1">1</option>
                <option value="2">2</option>
                </select>
                </p>
                <label for="entr/exi">Entrance/Exit</label>
                <input type="text" name="ent" id="ent" value="objJSON.name" placeholder="enter entrance/exit"/>
                <p>
                <label for="type">Type</label>
                <input type="text" name="type" value="objJSON.type" placeholder="enter your work station"/>
      <label for="name">Name</label>
      <input type="text" id="workstnname" name="workstnname" placeholder="enter your work station" onblur="return name();" onkeypress="return onKeyPressBlockNumbers(event);">
      <label for="description">Description</label>
      <textarea name="description" style="height:150px;width:300px;"></textarea>
      <label for="machinetype">Machine Type</label>
                <select style="width: 305px;text-align:left;"  name="machinetype">
                <option value="">Select</option>
                <option value="kiosk">kiosk</option>
                <option value="workstation">workstation</option>
              </select>
                <p>
                <input type="submit" name="submit" value="Submit">
                </p>
    </form>
  </div>

我没有得到pentance或exit和type键的值json-iam得到响应并被提醒是

[{"name":"Default Entrance + Exit","type":"both"}]

我不知道我是否在代码中做了一些模糊处理,因为我刚开始使用javascript感谢

出于安全和工作流的原因,最好通过json解析json。parse

var objJSON = JSON.parse(strJSON);

不是通过eval

试试这个,

  var objJSON = JSON.parse(result);
  alert(objJSON.name);
  alert(objJSON.type);

请尝试这个

 JSONObject obj = new JSONObject(result);
    List<String> list = new ArrayList<String>();
    JSONArray array = obj.getJSONArray("interests");
    for(int i = 0 ; i < array.length() ; i++){
        list.add(array.getJSONObject(i).getString("interestKey"));
    }

使用org.json库