如何从JSON中获取(解码)结果对象



我已经使用JSON_encode()从数组中创建了一个字符串,但我一直不知道如何在编码后获得数据。我正在通过jQuery访问结果,但我对格式化感到困惑

结果:

froms: "[{"campaign_froms":"that"},{"campaign_froms":"why r"},{"campaign_froms":"thatr"},{"campaign_froms":"thisr"}]"
subjects: "[{"campaign_subjects":"hi"},{"campaign_subjects":"hir"}]"

jQuery:

$.ajax({
      dataType: "json",
      type: "GET",
      url: "campaignDetails.php",
      data: dataString,
      success: function(response){
          $('#offer_title').text(response["data"]["campaign_name"]);
          $('#offer_link').val(response["data"]["offer_link"]);
          $('#optout_link').val(response["data"]["optout_link"]);
          $('#campaign_image').val(response["data"]["campaign_image"]);
          $('#optout_image').val(response["data"]["optout_image"]);
          $('#subjects').val(response["subjects"]["campaign_subjects"]);
          $('#froms').val(response["froms"]["campaign_froms"]);
          $('#campaign_id').val(response["data"]["id"]);
          $('.campaignImageViewer').attr("src", imageUrl + response["data"]["campaign_image"]);
          $('.optoutImageViewer').attr("src", imageUrl + response["data"]["optout_image"]);
          //alert(response[0]["offer_link"]);
          console.log(response);
     }
});

使用jQuery.parseJSON()函数将其解码为JS对象。

所以,更改这一行:

success: function(response) {

进入:

success: function(data) {
    response = jQuery.parseJSON(data);

这将得到data的结果,并将其转换为您使用的JS对象response。然后你的第二块代码应该可以工作了。

最新更新