[英]how to effeciantly format json data
I am trying to format json data in the most efficient way.我正在尝试以最有效的方式格式化 json 数据。 I need to format 2 different keys in a object.我需要在一个对象中格式化 2 个不同的键。 both are strings I made a fiddle with the json fiddle两者都是我用 json小提琴制作的字符串
var json = [
{
"Id": 1,
"ReportId": 1,
"ReportName": "HBO: {MSA}"
"QueueDate": "2015-08-25T17:16:54.233",
"Parameters": "<xml><CBSA>Abilene, TX</CBSA><CBSASK>3</CBSASK><CBSAID>1203</CBSAID><CBSACode>10180</CBSACode><MonthNumber>6</MonthNumber><Month>June</Month><Year>2015</Year><FromDate>06/01/2014</FromDate><ToDate>06/01/2015</ToDate></xml>"
},
{
"Id": 2,
"ReportId": 2,
"ReportName": "HBO:{Marvin}"
"QueueDate": "2015-08-25T17:20:50.463",
"Parameters": "<xml><CBSA>Abilene, TX</CBSA><CBSASK>3</CBSASK><CBSAID>1203</CBSAID><CBSACode>10180</CBSACode><MonthNumber>6</MonthNumber><Month>June</Month><Year>2015</Year><FromDate>06/01/2014</FromDate><ToDate>06/01/2015</ToDate></xml>"
},
{
"Id": 3,
"ReportId": 2,
"ReportName": "HBO:{Marvin}"
"QueueDate": "2015-08-25T17:23:58.377",
"Parameters": "<xml><CBSA>Abilene, TX</CBSA><CBSASK>3</CBSASK><CBSAID>1203</CBSAID><CBSACode>10180</CBSACode><MonthNumber>6</MonthNumber><Month>June</Month><Year>2015</Year><FromDate>06/01/2014</FromDate><ToDate>06/01/2015</ToDate></xml>"
}
] ]
I need to end up with:我需要结束:
ReportName: 'MSA',
Parameters: 'Abilene, Tx'
You can use $.parseXML()
to convert the xml string to an xml document object, then wrap that in $()
to use jQuery traverse methods on it您可以使用$.parseXML()
将 xml 字符串转换为 xml 文档对象,然后将其包装在$()
以在其上使用 jQuery 遍历方法
$.each(json, function (_, item) {
var $xml = $($.parseXML(item.Parameters));
var cbsa = $xml.find('CBSA').text();
$('body').append($('<p>').text(cbsa));
});
Reference: parseXML() docs参考: parseXML() 文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.