You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

新手求教:如何从Supreme JSON接口获取数据并查找指定名称条目

Hey there! Let's break this down step by step since you're new to working with this specific API and web data collection. I'll cover the full implementation flow, fix the issue with your $.getJSON code, and share some learning resources to build your skills.

1. Full Implementation Flow

Here's the complete process to fetch the data, find your target item, and extract its details:

  • Step 1: Fetch the JSON data from the API endpoint
  • Step 2: Parse and store the returned array (note: the API actually returns an object containing a products array)
  • Step 3: Traverse the array to find the item named "example"
  • Step 4: Extract and use the related information from that item
2. Fixing Your $.getJSON Issue

Your initial code was cut off, so let's write the full, working version. Common issues here include incomplete callbacks, unhandled errors, or cross-origin resource sharing (CORS) blocks if you're running the code from a local HTML file.

Here's the corrected code:

// Initialize an empty array to hold products
let products = [];

// Fetch the JSON data with $.getJSON
$.getJSON('http://www.supremenewyork.com/mobile_stock.json')
  .done(function(data) {
    // Store the products array from the API response
    products = data.products;
    console.log("Fetched all products:", products);

    // Find the first item named "example"
    const targetItem = products.find(product => product.name === "example");

    if (targetItem) {
      console.log("Found target item:", targetItem);
      // Extract related info (adjust properties based on the API's actual response structure)
      console.log("Price:", targetItem.price);
      console.log("Image URL:", targetItem.image_url);
      console.log("Product Page URL:", targetItem.url);
    } else {
      console.log("No item named 'example' found in the product list");
    }
  })
  .fail(function(jqxhr, textStatus, error) {
    // Handle request failures (e.g., network issues, CORS errors)
    const errorMsg = `${textStatus}: ${error}`;
    console.log("Request failed:", errorMsg);
    // If you hit a CORS error, try running your code on a local server (like Python's SimpleHTTPServer) or use a backend proxy
  });
3. How to Traverse the Array for Your Target Item

We used the find() method above because it's the most concise way to get the first matching item. If you need to find all items named "example" (not just the first), use filter() instead:

const allMatchingItems = products.filter(product => product.name === "example");
console.log("All items named 'example':", allMatchingItems);

Alternatively, if you prefer a more explicit loop, you can use forEach():

products.forEach(product => {
  if (product.name === "example") {
    console.log("Found matching item:", product);
    // Extract your desired info here
  }
});
4. Learning Resources to Build Your Skills
  • JavaScript Array Methods: Master find(), filter(), forEach(), and other array tools. The MDN JavaScript documentation has clear, practical explanations and examples for each method.
  • jQuery AJAX: Deepen your understanding of $.getJSON and jQuery's AJAX capabilities by exploring official jQuery guides on request handling and error management.
  • Web Data Collection Basics: Learn about CORS (why you might get blocked when running code locally) and the difference between browser-side and server-side data collection. For server-side scraping, tools like Node.js (with axios or node-fetch) or Python (with requests) are great starting points.

内容的提问来源于stack exchange,提问作者Edward Chen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:12:18