javascript - 如何解析 URL 的 json 输出

标签 javascript jquery json url

我正在尝试从 jQuery 中的 URL 解析 json 请求,但似乎无法正确处理。

下面是我所拥有的,但它不起作用。我正在尝试解析 post_title 和内容,并使用 getElementById 方法将其输出到 div 中。

下面是代码和 Json 请求。

        <div id="test"></div>
    <span>This is cool.</span>

<script>
jQuery(document).ready(function($) {
  $.ajax({
  url : "https//website-API-json.com/string",
  dataType : "jsonp",
  success : function(parsed_json) {
  var title = parsed_json{result"result"[{"post_title"}};
  var content = parsed_json['result']['']['post_content'];

      document.getElementById("test").innerHTML = 'div class:"post_title"> ' + title + ' </div><br><div class:"post_content"> ' + content '</div>'
  }


  });
});

这是Json请求结果

result({
   "respond":1,
   "paging":{
      "stillmore":0,
      "perpage":10,
      "callpage":1,
      "next":2,
      "previous":0,
      "pages":1,
      "result":"1"
   },
   "message":"",
   "result":[
      {
         "ID":"2003",
         "post_title":"Higher Grounds Coffee Shop",
         "guid":"",
         "post_content":"\"spring fever has hit the high country, stop in for a refreshing frappe, smoothie or a delicious milkshake\"",
         "post_author":"2",
         "post_excerpt":"",
         "post_date":"May 1, 2015",
         "post_date_gmt":"May 2, 2015",
         "comment_status":"open",
         "ping_status":"open",
         "post_name":"201-new-market-centre-boone-nc-28607-united-states-higher-grounds-coffee-shop",
         "post_modified":"May 1, 2015",
         "post_modified_gmt":"May 2, 2015",
         "post_content_filtered":"",
         "post_parent":"0",
         "menu_order":"0",
         "comment_count":"3",
         "featuredimage":"",
         "featuredthumb":"",
         "taxonomies":{
            "job_listing_region":[
               {
                  "id":"157",
                  "name":"Charlotte",
                  "slug":"charlotte-nc",
                  "description":"",
                  "count":"1",
                  "parent":"104",
                  "image":"",
                  "custom_fields":[

                  ]
               }
            ]
         },
         "category":[

         ],
         "tags":[

         ],
         "author":[
            {
               "ID":"2",
               "user_login":"Eric",
               "user_nicename":"ericgriffin",
               "user_email":"",
               "user_url":"",
               "user_registered":"May 15, 2015",
               "display_name":"Eric",
               "role":{
                  "customer":true
               },
               "first_name":"Eric",
               "last_name":"Griffin",
               "nickname":"EricGriffin",
               "description":"",
               "avatar":"",
               "aim":"",
               "jabber":"",
               "yim":"",
               "custom_fields":{
                  "company_name":"",
                  "avatar":"",
                  "hasbeensetup":"IsSetup"
               }
            }
         ],
         "post_format":false,
         "custom_fields":{
            "favorites":"0"
         }
      }
   ]
})

任何帮助将不胜感激。我正在尝试提取 post_title 和 post_content。

最佳答案

使用jQuery.getJSON() 。它会为您解析 JSON。另外,我一直觉得 success 回调既烦人又丑陋:这就是我要做的:

function appendPost(__, post) {
  var title = $('<div/>')
    .addClass('post_title')
    .text(post.post_title);
  var content = $('<div/>')
    .addClass('post_content')
    .text(post.post_content);
  $('#test')
    .append(title)
    .append('<br/>')
    .append(content);
}

function processResults(parsed_json) {
  var posts = parsed_json.result || [];
  $('#test').empty();
  $.each(posts, appendPost);
}

jQuery(document).ready(function($) {
  $.getJSON("https//website-API-json.com/string")
    .then(processResults);
});

关于javascript - 如何解析 URL 的 json 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30419489/

相关文章:

javascript - 如何获得连续的柱形图?

javascript - 阻止输入文本字段中的字符,将输入镜像到 span 或 div

php - 如何从 MySQL 数据库检索选定的行并将信息发送回 Android?

json - Flutter 如何在本地保存列表数据

javascript - 使用D3.js绘制自定义json映射

Javascript onkeydown 事件只触发一次?

javascript - 如何在 Visual Studio Code 中使用 TypeScript 和 JavaScript 禁用自动导入的自动分号?

JavaScript 全局变量的替代方案

javascript - 切换菜单 - 使菜单在当前页面保持打开状态

javascript - React 中如何集成 jQuery 插件