下面是我的json字符串,我想将additionalDetails的数据转换为json字符串,但我无法成功。
{
"docs": 70,
"size": 250,
"currentPageNo": 0,
"recordStartFrom": 0,
"columnHeader": [
{
"id": "0",
"fieldName": "id",
"imgName": "",
"tooltipSrc": "",
}
],
"data": [
{
"Number": "10000",
"price": "4.75",
"manfName": "",
"minOrderQty": "0.00",
"maxOrderQty": "0",
"additionalDetails": "<item>CUR:Rupees</item><item>code:one</item>",
},
{
"Number": "10001",
"price": "1.75",
"manfName": "",
"minOrderQty": "0.00",
"maxOrderQty": "0",
"additionalDetails": "",
}
]
}
我正在尝试将additionalDetails数据转换为json字符串。我尝试将json字符串转换为XML内容,将XML转换为json字符串,但additionalDetails的数据不会转换为json字符串。
答案 0 :(得分:0)
这不是所述问题的一般解决方案,而是解决您的问题。
我会使用正则表达式:
<item>([0-9a-zA-Z]*):([0-9a-zA-Z]*)</item>
如果在全局模式下使用此表达式,则将匹配每个输入。您可以使用java.util.regex.Pattern解析字符串。 (请参阅“组和捕获”部分以获取其中的值。)
然后,您可以构建com.google.gson.JsonArray并填充匹配器中的值。然后可以使用additionalDetails
字段替换此数组。
最后,您只需写出结果或做任何您想做的事情。
答案 1 :(得分:0)
我使用FIDDLE库为您准备了一些X2JS:http://code.google.com/p/x2js/
<强> INDEX.HTML:强>
<body>
<pre id="pre_id">TEST</pre>
<button id="go_button">GO</button>
</body>
<强> SCRIPT:强>
var x2js = new X2JS();
$("#go_button").click(function() {
var form_data = {
"docs": 70,
"size": 250,
"currentPageNo": 0,
"recordStartFrom": 0,
"columnHeader": [
{
"id": "0",
"fieldName": "id",
"imgName": "",
"tooltipSrc": "",
}
],
"data": [
{
"Number": "10000",
"price": "4.75",
"manfName": "",
"minOrderQty": "0.00",
"maxOrderQty": "0",
"additionalDetails": "<item>CUR:Rupees</item><item>code:one</item>",
},
{
"Number": "10001",
"price": "1.75",
"manfName": "",
"minOrderQty": "0.00",
"maxOrderQty": "0",
"additionalDetails": "",
}
]
};
for (var i=0; i<form_data.data.length; i++) {
console.log(x2js.xml_str2json(
'<additionalDetails>'+
form_data.data[i].additionalDetails+
'</additionalDetails>'));
form_data.data[i].additionalDetails = x2js.xml_str2json(
'<additionalDetails>'+
form_data.data[i].additionalDetails+
'</additionalDetails>').additionalDetails;
}
$('#pre_id').html(JSON.stringify(form_data));
});