这可能是一个简单的问题,但我想知道如何获得JSON Web服务以返回具有以下格式的字符串:
var invoice = {
invoiceItems: [
{ type: 'item',
part: '99Designs', description: '99 Designs Logo',
price: 450.00, qty: 1 },
{ type: 'service',
service: 'Web development and testing',
price: 25000.00 },
{ type: 'item',
part: 'LinodeMonthly', description: 'Monthly site hosting',
price: 40.00, qty: 12 }
]
};
我的服务现在返回的是List,它看起来像
[1,2,2,4444]
[13,444,233]
etc
“:”之前的部分会自动附加还是我需要在网络服务中手动执行?
答案 0 :(得分:3)
JSON只是具有特定语法的 text 。这意味着您的Web服务需要输出看起来完全相同的 text :
{
invoiceItems: [
{ type: 'item',
part: '99Designs', description: '99 Designs Logo',
price: 450.00, qty: 1 },
{ type: 'service',
service: 'Web development and testing',
price: 25000.00 },
{ type: 'item',
part: 'LinodeMonthly', description: 'Monthly site hosting',
price: 40.00, qty: 12 }
]
}
根据用于Web服务的语言,您很可能会找到将对象转换为JSON格式的有用库(它将像专用的.toString()
一样工作)。但是你必须将你现在拥有的列表转换为更像Map的东西,以便库自动输出JSON。
或者,您可以通过迭代列表并简单地将该字符串作为输出来构建自己的字符串。