dqyknf4423 2012-03-21 20:53
浏览 672

如何在HTML中输出JSON数组

I use ECWID for my website store and am wanting to create some code, to do that i have to do some JSON requests and i have never worked with JSON. When i type the following into my address bar: http://app.ecwid.com/api/v1/STOREID/product?id=PRODUCTID ofcourse replacing storeid and productid with numbers referencing to my store and the specific product i get a text return of

{
  "id": 8443685,
  "sku": "KEN000025",
  "smallThumbnailUrl": "picture URL here",
  "thumbnailUrl": "thumbnail URL here",
  "imageUrl": "image URL here",
  "name": "Kenwood Excelon KFC-X173",
  "price": 99.99,
  "weight": 1.0,
  "url": "long URL here",
  "description": "long description here",
  "options": [],
  "taxes": [],
  "galleryImages": [],
  "categories": [
    {
      "id": 769355,
      "thumbnailUrl": "url here",
      "name": "Speakers",
      "url": "url here",
      "description": ""
    },
    {
      "id": 1466304,
      "parentId": 1466305,
      "thumbnailUrl": "URL here",
      "name": "Kenwood",
      "url": "URL here",
      "description": ""
    }
  ],
  "dateAdded": 1325037351
}

Now if i have an HTML document and i want to output the picture of an item then below that the price of it how would i do that using the information from the JSON link.

PS i know Java script HTML PHP XML and a lot of other languages just not JSON Thank you

  • 写回答

3条回答 默认 最新

  • douyanpeng0748 2012-03-21 21:18
    关注

    You can decode the JSON with PHP, which will turn it into associative arrays which you can then iterate through to display the data:

    You should be able to use something like:

    $productArray = json_decode( $JSON_DATA_VARIABLE, true );
    $productImageOne = $productArray['categories'][0]['thumbnailUrl'];
    $productImageTwo = $productArray['categories'][1]['thumbnailUrl'];
    $price = $productArray['price'];
    

    You will have to set the JSON data to a variable before you can decode it. I'm sure that example isn't 100% correct (I'm not great at going through arrays without a working example), but you get the idea. http://www.php.net/manual/en/function.json-decode.php

    评论

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制