String xtext = "#Samsung Electric Range with Fan Convection~86309930018~$525.00~TRUCK~0.0~R55856112#HAUL AWAY~07601000018~$24.99~TRUCK~0.0~R55856112#ELECTRIC RANGE C~88301170018~$21.99~TRUCK~0.0~R55856112";
需要Java代码将此字符串转换为json格式,如下所示。 行和行的分隔符#相同。 注意:我不知道Java,我需要将此代码插入talend tjavarow组件中。请帮忙。
{ "items":[{ "description":"Samsung Electric Range with Fan Convection",
"id":"86309930018",
"price": { "unitPrice":525.00 },
"shipMethod":"TRUCK",
"ivn":"R55856112",
"quantity":0.0
},
{ "description":"HAUL AWAY",
"id":"07601000018",
"price": { "unitPrice":24.99 },
"shipMethod":"TRUCK",
"ivn":"R55856112",
"quantity":0.0
},
{
"description":"ELECTRIC RANGE C",
"id":"88301170018",
"price": { "unitPrice":21.99 },
"shipMethod":"TRUCK",
"ivn":"R55856112",
"quantity":0.0
}
]
}
明白了...
String str = "Samsung Electric Range with Fan Convection~86309930018~$525.00~TRUCK~0.0~R55856112#HAUL AWAY~07601000018~$24.99~TRUCK~0.0~R55856112#ELECTRIC RANGE C~88301170018~$21.99~TRUCK~0.0~R55856112";
String[] aStr;
String[] bStr;
String desc;
String id;
String price;
String shipMethod;
String ivn;
String quantity;
aStr = str.split("#");
for (String a : aStr )
{
System.out.println(a);
bStr = a.split("~");
desc = bStr[0];
id = bStr[1];
price = bStr[2];
shipMethod = bStr[3];
ivn = bStr[4];
quantity = bStr[5];
System.out.println(desc+"|"+id+"|"+price+"|"+shipMethod+"|"+ivn+"|"+quantity);
}
答案 0 :(得分:1)
您需要将字符串切成数组
String [ ] split ( String regex, int limit )
首先拉出#和〜之间的字符串 取剩余的字符串并使用“〜”作为分隔符将其拆分
id = array[0]
price = array[1]
shipMethod = array[2]
ivn = array[3]
quantity = array[4]
然后您可以将其全部打包到json