XML API子返回函数

本文关键字:函数 返回 API XML | 更新日期: 2023-09-27 18:17:33

我正在对返回XML的控制器执行API调用。输出为:

<ArrayOfreport_overallTime>
    <report_overallTime>
        <label>United States</label>
        <value>65</value>
    </report_overallTime>
    <report_overallTime>
        <label>United Kingdom</label>
        <value>58</value>
    </report_overallTime>
    <report_overallTime>
        <label>Germany</label>
        <value>30</value>
    </report_overallTime>
    <report_overallTime>
        <label>India</label>
        <value>65</value>
    </report_overallTime>
    <report_overallTime>
        <label>Russia</label>
        <value>65</value>
    </report_overallTime>
    <report_overallTime>
        <label>Germany</label>
        <value>30</value>
    </report_overallTime>
</ArrayOfreport_overallTime>

这是由以下c#控制器代码生成的:

public class report_overallTimeController : ApiController
{
    public void report_overallTime(HttpContext context)
    {
        var request = HttpContext.Current.Request;
        var settings = Properties.Settings.Default;
        GetAllItems();
    }
    report_overallTime[] items = new report_overallTime[]
    {
        new report_overallTime { label = "United States", value = "65" },
        new report_overallTime { label = "United Kingdom", value = "58" },
        new report_overallTime { label = "Germany", value = "30" },
        new report_overallTime { label = "India", value = "65" },
        new report_overallTime { label = "Russia", value = "65" },
        new report_overallTime { label = "Germany", value = "30" }
    };
    public IEnumerable<report_overallTime> GetAllItems()
    {
        return items;
    }       
}

在我的jQuery中,我试图获得标签/值对并将它们插入数组中。

function initChart(xml) {
    var data = [];
    //alert((new XMLSerializer()).serializeToString(xml));
    $(xml).find('ArrayOfreport_overallTime report_overallTime').each(function (index) {
        var label = $(this).children("label").text;
        var value = $(this).children("value").text;
        data.push([label, parseFloat(value)]);
    });
    //Do something with data...
}

当我尝试输出var label的值时,我得到以下警告:

function(a) {
    return Y(this, function(a) {
        return void 0 === a ? n.text(this) : this.empty().append((this[0] && this[0].ownerDocument || d).createTextNode(a))
    }, null, a, arguments.length)
}

我做错了什么?我期待着与国家联系。

XML API子返回函数

问题是因为ArrayOfreport_overallTime元素是XML字符串的根,所以您需要使用filter()来检索它,然后使用find()来获取report_overallTime。您也可以使用map()来构建您的数组。试试这个:

var xml = '<ArrayOfreport_overallTime><report_overallTime><label>United States</label><value>65</value></report_overallTime><report_overallTime><label>United Kingdom</label><value>58</value></report_overallTime><report_overallTime><label>Germany</label><value>30</value></report_overallTime><report_overallTime><label>India</label><value>65</value></report_overallTime><report_overallTime><label>Russia</label><value>65</value></report_overallTime><report_overallTime><label>Germany</label><value>30</value></report_overallTime></ArrayOfreport_overallTime>';
var data = $(xml).filter('ArrayOfreport_overallTime').find('report_overallTime').map(function() {
    var $el = $(this);
    return [[$el.children("label").text(), parseFloat($el.children("value").text())]];
}).get();
console.log(data);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>