如何解析大型嵌套的json对象?

时间:2019-03-11 03:43:25

标签: javascript json

PasteBin JSON

我想得到这个对象,因为它说jsonlint有效,但解析不是任何人都会帮助的

"Data":[{...},{...},] // structure build like this

当我尝试

JSON.parse(jsonparamter) <-- Uncaught SyntaxError: Unexpected token A in JSON at position 71
    at JSON.parse (<anonymous>)
    at <anonymous>:1:6

1 个答案:

答案 0 :(得分:0)

有多个级别的JSON编码数据,因此您将必须创建一个循环来解码JSON嵌套中更深的元素。使用以下代码查看在此字典中访问Data.Adress.Value的示例

org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "tx.Broker" (template: "index2" - line 42, col 59)
    at org.thymeleaf.spring5.expression.SPELVariableExpressionEvaluator.evaluate(SPELVariableExpressionEvaluator.java:290) ~[thymeleaf-spring5-3.0.11.RELEASE.jar:3.0.11.RELEASE]
    at 
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'Broker' cannot be found on null
    at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:213) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:104) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.expression.spel.ast.PropertyOrFieldReference.access$000(PropertyOrFieldReference.java:51) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.expression.spel.ast.PropertyOrFieldReference$AccessorLValue.getValue(PropertyOrFieldReference.java:406) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:90) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:109) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:328) ~[spring-expression-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.thymeleaf.spring5.expression.SPELVariableExpressionEvaluator.evaluate(SPELVariableExpressionEvaluator.java:263) ~[thymeleaf-spring5-3.0.11.RELEASE.jar:3.0.11.RELEASE]
    ... 68 common frames omitted
// set up urls and headers for making HTTP req
corsurl = 'https://cors-anywhere.herokuapp.com/'
jsonurl = 'https://pastebin.com/raw/vuecweML'
headerNames = ['Content-Type','Accept']
headerValues = [ 'application/json', 'application/json']


// Modular get request function that I use
function getRequest (baseRestURL, APIPath, headerNames, headerValues, callback) {
  var completeRestURL = baseRestURL + APIPath
  console.log('REST API URL: ' + completeRestURL)
  var method = 'GET'
  var url = completeRestURL
  var async = true
  var request2 = new XMLHttpRequest()
  request2.onload = function () {
    console.log('ONLOAD')
    var status = request2.status // HTTP response status, e.g., 200 for "200 OK"
    console.log(status)
    console.log(request2.responseText)
    var response = request2.responseText
    return callback(response)
  }
  request2.open(method, url, async)
  for (var i in headerNames) {
    request2.setRequestHeader(headerNames[i], headerValues[i])
  }
  request2.send(null)
 }
  
  
  
// Our code of interest  
getRequest(corsurl, jsonurl, headerNames, headerValues, response => {

  parsed = JSON.parse(response).Data //parse our data the first time, and get the data attribute from dictionary
  objects = JSON.parse(parsed) // parse a second time ( as data is JSON encoded twice )
  selection = JSON.parse(objects[0].Address)[0].Value // parse a third time and select an attribute
  document.getElementById('result').innerHTML = selection // Add it to our html to display
 })