JSON使用$ .each

时间:2015-06-10 07:05:11

标签: javascript jquery json

以下是JSON响应示例。我需要以通用方式解析它,而不是使用transactionList.transaction [0]。

"rateType": interestonly,
"relationshipId": consumer,
"sourceCode": null,
"subType": null,
"transactionList": {
    "transaction": [
      {
        "amount": {
          "currencyCode": "USD",
          "value": 1968.99
        },
        "customData": {
          "valuePair": [
            {
              "name": "valuePair",
              "value": "001"
            }
          ]
        },
        "dateTimePosted": null,
        "description": "xyz",
        "id": "01",
        "interestAmount": {
          "currencyCode": "USD",
          "value": 1250
        },
        "merchantCategoryCode": 987654321,
        "principalAmount": {
          "currencyCode": "USD",
          "value": 1823.8
        },
        "source": "Mobile Deposit",
        "status": "Posted",
        "type": "1"
      }
    ]
  },

我使用以下代码来解析json

$.each(jsonDataArr, recursive);

function recursive(key, val) {
    if (val instanceof Object) {
        list += "<tr><td colspan='2'>";
        list += key + "</td></tr>";
        $.each(val, recursive);
    } else {
        if(val != null) {
            if(!val.hasOwnProperty(key)) {
                list += "<tr><td>" + key + "</td><td>" + val + "</td></tr>";
            }
        }
    }
}

并输出为 transactionList 交易 0 然后是其他键&amp;值。我希望获得transactionList以及所有键和值,而不是获取事务和数组元素。所以我猜我的解析逻辑不正确。任何人都可以帮我解决这个问题所以我可以显示transactionList吗?感谢您的帮助。

2 个答案:

答案 0 :(得分:3)

如果我们有一个你想要的结果的例子会有所帮助。 如果transactionList中有多个事务,它将如何显示怎么办?

基本上你的问题是Arrays也是对象。

http://jsfiddle.net/v0gcroou/

if (transactionList.transaction instanceof Object) == true
Key of transactionList.transaction is 0

相反,您还需要测试对象是否是数组,并根据您现在正在解析数组而不是字符串或JSON对象的事实来执行其他操作

(Object.prototype.toString.call(val) === '[object Array]')

另一个简单的方法是检查'number' === typeof key,因为您的JSON对象不包含数字键,但数组对象固有。

http://jsfiddle.net/h66tsm9u/

答案 1 :(得分:2)

您希望显示包含所有数据的表格。我在表格中添加了border = 1来显示框。请参阅http://output.jsbin.com/wuwoga/7/embed?js,output

中的示例
function display(data) {
  var html = "<table border='1'>";
  var lists = recursive(data);
  html += lists + "</table>";
  return html;
}

function recursive(json) {
  var list = "";
  var instanceObj = false;

  $.each(json, function(key, val){
    instanceObj = (val instanceof Object);

    list += [
      "<tr>",
      "<td>" + key + "</td>",
      (instanceObj) ? 
         "<td><table border='1'>" + recursive(val) + "</table></td>" :
             "<td>" + val + "</td>",
      "</tr>"
    ].join("");
  });

  return list;
}

如果您使用下面的json调用display(json),则会显示所有数据。如果在事务数组中添加更多数据,它也会显示

var json = {
  "rateType": "interestonly",
  "relationshipId": "consumer",
  "sourceCode": null,
  "subType": null,
  "transactionList": {
    "transaction": [
      {
    "amount": {
      "currencyCode": "USD",
      "value": 1968.99
    },
    "customData": {
      "valuePair": [
        {
          "name": "valuePair",
          "value": "001"
        }
      ]
    },
    "dateTimePosted": null,
    "description": "xyz",
    "id": "01",
    "interestAmount": {
      "currencyCode": "USD",
      "value": 1250
    },
    "merchantCategoryCode": 987654321,
    "principalAmount": {
      "currencyCode": "USD",
      "value": 1823.8
    },
    "source": "Mobile Deposit",
    "status": "Posted",
    "type": "1"
      }
    ]
  }
};