String dummyStr="[{\"Emp_Id\":\"1254\",\"Emp_Name\":\"abcd\"},{\"Emp_Id\":\"1234\",\"Emp_Name\":\"efgh\"}]";
System.out.println("The JSON string is"+dummyStr);
提供以下输出:
JSON字符串是[{" Emp_Id":" 1254"," Emp_Name":" abcd"},{&#34 ; EMP_ID":" 1234"" EMP_NAME":" EFGH"}]
****但是当我试图加入一个arraylist时,它显示为****
tmpJsonArrayList.put(dummyStr)
System.out.println("The JSON string List is"+tmpJsonArrayList);
JSON字符串列表是[" [{\" Emp_Id \":\" 1254 \",\" Emp_Name \" :\" ABCD \"},{\" EMP_ID \":\" 1234 \" \" EMP_NAME \" :\" EFGH \"}]"]
答案 0 :(得分:0)
您可以使用JsonParser将字符串转换为json数组:
String dummyStr="[{\"Emp_Id\":\"1254\",\"Emp_Name\":\"abcd\"},{\"Emp_Id\":\"1234\",\"Emp_Name\":\"efgh\"}]";
JsonParser parser = new JsonParser();
// parse the string as JsonArray
JsonArray tmpJsonArrayList = (JsonArray) parser.parse(dummyStr);
System.out.println("The JSON string List is"+tmpJsonArrayList);
答案 1 :(得分:0)
String dummyStr="[{\"Emp_Id\":\"1254\",\"Emp_Name\":\"abcd\"},{\"Emp_Id\":\"1234\",\"Emp_Name\":\"efgh\"}]";
JsonParser parser = new JsonParser();
JsonElement jsonElement = parser.parse(dummyStr);
JsonArray tmpJsonArrayList = jsonElement.getAsJsonArray();
System.out.println("The JSON string List is"+tmpJsonArrayList);
这是我们在java中解析json字符串的方法。您需要添加com.google.gson
库来编译此代码。