57617

Proper ajax call for JSON data

Question:

First time trying to retrieve data from a JSON file and honestly have no idea how to get the objects I would like to get.

Can someone please tell me how I can grab values such as Family Name or familyAttributes? I just have no idea how to process the data.

<strong>Current AJAX Call</strong>

$.ajax({ type: 'GET', url: 'https://somesite.com/EloMS.json', data: { category : 'Touchmonitors' }, dataType: 'json', success: function(data) { $.each(data, function(index, element) { console.log(element); }); } });

<strong>JSON Syntax:</strong>

{ "products": [ { "Family Name": "3201L", "Type": "IDS", "Size (inches)": 32, "Aspect Ratio": "16:9", "Part Number": "E415988", "Product Description": "ET3201L-8UWA-0-MT-GY-G", "Marketing Description": "3201L 32-inch wide LCD Monitor, VGA, HDMI & DisplayPort video interface, 01 series enhanced AV, IntelliTouch Plus USB touch controller interface, Worldwide-version, Clear, Gray ", "Advance Unit Replacement": "", "Elo Elite": "", "Package Quantity": 1, "Minimum Order Quantity": 1, "List Price": 1800 }, { ... } ] "families": [ { "category": "Touchmonitors", "types": [ "Desktop", "Display", "Open Frame" ], "image": "", "familyAttributes": [ { "type": "Display", "image": "", "families": [ { "familyName": "0700L", "image": "" } ] }, { ... } } ] }

I've tried several variations of element.category but haven't been able to get any data other than the full objects.

Answer1:

For the product object, you'd be wanting:

data.products[n]["Family Name"]

where n is an integer offset into the products array.

For familyAttributes you need:

data.families[n].familyAttributes

The ["string"] syntax is necessary in the first case because of the space character in the key.

Answer2:

You can use getJSON(), the documentation is here: <a href="http://api.jquery.com/jquery.getjson/" rel="nofollow">http://api.jquery.com/jquery.getjson/</a>

You can access to your model like so:

$.getJSON('https://somesite.com/EloMS.json', function(data) { // Products table var products = data.products; // Product type var typeProduct = data.products[0].Type; // familyAttributes var familyAttributes = data.families[0].familyAttributes; // Family Name var familyName = data.products[0]['Family Name']; });

Recommend

  • Emulate low resolution device on the chrome dev tool
  • Why does Android's Nexus 7 virtual device not load my “large” layout resources?
  • Update to Xcode 5.1 and layout for 4 inches does not works fine
  • Javascript: adding an integer and decimal giving wrong answer
  • How to keep elements aligned regardless of browser zoom ratio?
  • flex-shrink behaviour in different browsers [duplicate]
  • Where to get negative sample images for Haar training? [closed]
  • How can I create a list of elements with the same xpath using selenium with python?
  • KineticJS : scaling issue while using custom clipFunc on 2x pixel aspect display
  • Alternate (Free) to AOP PostSharp for method tracing (and exception)
  • Hadoop/map-reduce: Total time spent by all maps in occupied slots vs. Total time spent by all map ta
  • Displaying and sizing a grayscale from a QImage in Qt
  • getelement by class name for clicking
  • Python/Javascript: WYSIWYG html editor - Handle large documents fast and/or design theory
  • Is there a package like bigmemory in R that can deal with large list objects?
  • Knockout custom binding handler
  • Why isn't obj.style.left = “200px”; working in this code?
  • CXF JAXB JAXBEncoderDecoder unmarshalling error : unexpected element when having qualified elements
  • Hide HTML elements without javascript, only CSS
  • Python pickle not one-to-one: different pickles give same object
  • C: Incompatible pointer type initializing
  • why xml file does not aligned properly after append the string in beginning and end of the file usin
  • Make VS2015 use angular-cli ng at build time in a .NET project
  • Change multiple background-images with jQuery
  • Android screen density dpi vs ppi
  • DirectX11 ClearRenderTargetViewback with transparent buffer?
  • Where to put my custom functions in Wordpress?
  • Can a Chrome extension content script make an jQuery AJAX request for an html file that is itself a
  • Change an a tag attribute in JavaScript based on screen width
  • Upload files with Ajax and Jquery
  • Weird JavaScript statement, what does it mean?
  • Jquery - Jquery Wysiwyg return html as a string
  • How to pass list parameters for each object using Spring MVC?
  • SVN: Merging two branches together
  • Buffer size for converting unsigned long to string
  • AngularJs get employee from factory
  • Proper way to use connect-multiparty with express.js?
  • Append folder name and increment by 1 using batch script
  • Django query for large number of relationships
  • Binding checkboxes to object values in AngularJs