开发者

json for jquery tmpl

开发者 https://www.devze.com 2023-02-21 18:11 出处:网络
can i use this format JSON with jquery template? { \"ROWCOUNT\": 3, \"COLUMNS\": [ \"UTCODICE\", \"UT__NOME\",

can i use this format JSON with jquery template?

{
  "ROWCOUNT": 3,
  "COLUMNS": [
    "UTCODICE",
    "UT__NOME",
    "UT__COGN"
  ],
  "DATA": {
    "UTCODICE": [
      1088,
      1087,
      1086
    ],
  开发者_JS百科  "UT__NOME": [
      'Roberto',
      'Paolo',
      'Carlo'
    ],
    "UT__COGN": [
      'Gino',
      'Luigi',
      'Luca'
    ]
  }
}

... or...

{
  "COLUMNS": [
    "UTCODICE",
    "UT__NOME",
    "UT__COGN"
  ],
  "DATA": [
    [
      1088,
      'Roberto',
      'Gino'
    ],
    [
      1087,
      'Paolo',
      'Luigi'
    ],
    [
      1086,
      'Carlo',
      'Luca'
    ]
  ]
}

how can i say to jquery to start parsing from "DATA" tag?

many thanks!


The second one looks like it should work, but you'll want to change all of the single quotes (') to double-quotes (").

There's no way to tell jQuery where to start parsing, it does the whole thing in one shot. However, once it's parsed, you should be able to just do:

parsed_json.DATA

to access the DATA part of the object.


You'll need a copy of JSON2.js

https://github.com/douglascrockford/JSON-js

You can then do this:

var oJSON = JSON.parse(data);
var alPeople = oJSON.DATA

Where data is the above string

There is a good example here:

http://weblogs.asp.net/dwahlin/archive/2010/11/20/reducing-code-by-using-jquery-templates.aspx

e.g essentially:

<script id="peopleTemplate" type="text/x-jquery-tmpl">
  //your template here
</script>
<div id="peopleList"></div>

and then in your script parse the data using JQuery template

$('#peopleTemplate').tmpl(alPeople).appendTo('#peopleList');


Try this with the second JSON object:

<script id="dataTemplate" type="text/x-jquery-tmpl">
{{each DATA}}
<tr>
{{each $value }}
<td>${$value}</td>
{{/each}}
</tr>
{{/each}}
</script>

The template above creates a table row (tr) for each array in "DATA", then creates a table cell (td) for each value in each array.

Hope that helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消