新手求教:如何从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.
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
productsarray) - Step 3: Traverse the array to find the item named "example"
- Step 4: Extract and use the related information from that item
$.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 });
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 } });
- 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
$.getJSONand 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
axiosornode-fetch) or Python (withrequests) are great starting points.
内容的提问来源于stack exchange,提问作者Edward Chen

