我有包含4件事的字符串,我希望将每件事物存储在一个列表中。 该字符串包含从android到服务器的食品订单,在服务器中我必须解析它才能显示。 字符串看起来像:
[:ordername:ordertime:orderprice: orderquantity:]
对于1个订单案例,我想在名单列表中输入ordername,在时间列表中输入ordertime,在价目表中输入orderprice等等 如果超过1个订单,它将被逗号分隔,如
[:ordername:ordertime:orderprice:orderquantity:],[:ordername2:ordertime2:orderprice2:orderquantity2:]
我想在名单列表中输入ordername
,ordername2
,在时间列表中输入ordertime
,ordertime2
,在价格中输入orderprice
,orderprice2
列表等等。
这是我试过的
String orderlist=request.getParameter("orderlist"); // this is a string which is coming from android to server containing orders
char[] orderarray=orderlist.toCharArray(); //converting string to char array
int comma_counter = 0;
int comma_counter1 = 0;
for (int i=0; i < orderlist.length(); i++){
if (orderarray[i]==','){
comma_counter++;
}
}
System.out.println("counter is"+comma_counter);
System.out.println("order list length"+orderlist.length());
ArrayList <String> order_array_list = new ArrayList <String>();
int no=0;
String temp="";
for (int j=no; j<orderarray.length; j++){
System.out.println(" j is "+ j);
if(orderarray[j]!=','){
temp = temp+orderarray[j];
// System.out.println("temp is "+temp);
}
else if(orderarray[j]==','){
order_array_list.add(temp);
temp="";
no=j;
}
}
String []parts= null;
for(int i=0; i<order_array_list.size(); i++){
String array= order_array_list.get(i);
parts= array.split(":");
for(int j=0; j<parts.length; j++)
{
System.out.println(parts[j]);
}
}
答案 0 :(得分:2)
这样的事情会起作用:
Map <Integer, List <String>> map = new HashMap <>();
// Initialize the map
map.put(1, new ArrayList <String> ());
map.put(2, new ArrayList <String> ());
map.put(3, new ArrayList <String> ());
map.put(4, new ArrayList <String> ());
String str = "[:ordername:ordertime:orderprice:orderquantity:]," +
"[:ordername2:ordertime2:orderprice2:orderquantity2:]";
// loop through each order set
for (String s: str.split(","))
{
// remove any leading and trailing spaces
s = s.trim();
// remove the brackets
s = s.replaceAll("[\\[\\]]", "");
int i = 1;
// loop through each order component
for (String c: s.split(":"))
{
// remove any leading and trailing spaces
c = c.trim();
if (c.length() > 0)
{
map.get(i).add(c);
i++;
}
}
}
System.out.println(map);
<强>输出强>:
{1 = [ordername,ordername2],2 = [ordertime,ordertime2], 3 = [orderprice,orderprice2],4 = [orderquantity,orderquantity2]}
注意:为简单起见,我使用HashMap
来包含所有列表,但如果您愿意,可以在HashMap
之外创建4个列表。在这种情况下,您需要在if/else if
语句中包含if (c.length() > 0)
条件。
答案 1 :(得分:1)
这段代码可以帮助你
import java.util.ArrayList;
import java.util.List;
public class MainClass {
public static void main(String[] args) {
String input = "[:ordername:ordertime:orderprice:orderquantity:], [:ordername2:ordertime2:orderprice2:orderquantity2:]";
input = input.replace("[:", "");
input = input.replace(":]", "");
String[] inputArray = input.split(",");
List<String> nameList = new ArrayList<String>();
List<String> timeList = new ArrayList<String>();
List<String> priceList = new ArrayList<String>();
List<String> quantityList = new ArrayList<String>();
for (String order : inputArray) {
String[] orderDetails = order.split(":");
nameList.add(orderDetails[0]);
timeList.add(orderDetails[1]);
priceList.add(orderDetails[2]);
quantityList.add(orderDetails[3]);
}
}
}
如果您更关注性能,可以使用apache commons替换字符串。