包含多项键值对的字符串怎么转成 List<Map<string,String>
2017-09-28
展开全部
包含多项键值对的字符串怎么转成 List<Map<string,String>
package jsonToMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
/**
* 说明 json字符串 转成 Map/List
*/
public class JsonToMap {
public static void main(String[] args) {
//JSONArray
String jsonArrayData="[{\"a1\":\"12\",\"b1\":\"112\",\"c1\":\"132\",\"d1\":\"134\"},{\"a2\":\"12\",\"b2\":\"112\",\"c2\":\"132\",\"d2\":\"134\"},{\"a3\":\"12\",\"b3\":\"112\",\"c3\":\"132\",\"d3\":\"134\"}]";
JSONArray jsonArray = JSONArray.fromObject(jsonArrayData);
List<Map<String,Object>> mapListJson = (List)jsonArray;
for (int i = 0; i < mapListJson.size(); i++) {
Map<String,Object> obj=mapListJson.get(i);
for(Entry<String,Object> entry : obj.entrySet()){
String strkey1 = entry.getKey();
Object strval1 = entry.getValue();
System.out.println("KEY:"+strkey1+" --> Value:"+strval1+"\n");
}
}
// JSONObject
String jsonObjectData="{\"data1\":{\"a1\":\"12\",\"b1\":\"112\",\"c1\":\"132\",\"d1\":\"134\"},\"data2\":{\"a2\":\"12\",\"b2\":\"112\",\"c2\":\"132\",\"d2\":\"134\"},\"data3\":{\"a3\":\"12\",\"b3\":\"112\",\"c3\":\"132\",\"d3\":\"134\"}}";
JSONObject jsonObject = JSONObject.fromObject(jsonObjectData);
Map<String, Object> mapJson = JSONObject.fromObject(jsonObject);
for(Entry<String,Object> entry : mapJson.entrySet()){
Object strval1 = entry.getValue();
JSONObject jsonObjectStrval1 = JSONObject.fromObject(strval1);
Map<String, Object> mapJsonObjectStrval1 = JSONObject.fromObject(jsonObjectStrval1);
System.out.println("KEY:"+entry.getKey()+" --> Value:"+entry.getValue()+"\n");
for(Entry<String, Object> entry1:mapJsonObjectStrval1.entrySet()){
System.out.println("KEY:"+entry1.getKey()+" --> Value:"+entry1.getValue()+"\n");
}
}
}
}
package jsonToMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
/**
* 说明 json字符串 转成 Map/List
*/
public class JsonToMap {
public static void main(String[] args) {
//JSONArray
String jsonArrayData="[{\"a1\":\"12\",\"b1\":\"112\",\"c1\":\"132\",\"d1\":\"134\"},{\"a2\":\"12\",\"b2\":\"112\",\"c2\":\"132\",\"d2\":\"134\"},{\"a3\":\"12\",\"b3\":\"112\",\"c3\":\"132\",\"d3\":\"134\"}]";
JSONArray jsonArray = JSONArray.fromObject(jsonArrayData);
List<Map<String,Object>> mapListJson = (List)jsonArray;
for (int i = 0; i < mapListJson.size(); i++) {
Map<String,Object> obj=mapListJson.get(i);
for(Entry<String,Object> entry : obj.entrySet()){
String strkey1 = entry.getKey();
Object strval1 = entry.getValue();
System.out.println("KEY:"+strkey1+" --> Value:"+strval1+"\n");
}
}
// JSONObject
String jsonObjectData="{\"data1\":{\"a1\":\"12\",\"b1\":\"112\",\"c1\":\"132\",\"d1\":\"134\"},\"data2\":{\"a2\":\"12\",\"b2\":\"112\",\"c2\":\"132\",\"d2\":\"134\"},\"data3\":{\"a3\":\"12\",\"b3\":\"112\",\"c3\":\"132\",\"d3\":\"134\"}}";
JSONObject jsonObject = JSONObject.fromObject(jsonObjectData);
Map<String, Object> mapJson = JSONObject.fromObject(jsonObject);
for(Entry<String,Object> entry : mapJson.entrySet()){
Object strval1 = entry.getValue();
JSONObject jsonObjectStrval1 = JSONObject.fromObject(strval1);
Map<String, Object> mapJsonObjectStrval1 = JSONObject.fromObject(jsonObjectStrval1);
System.out.println("KEY:"+entry.getKey()+" --> Value:"+entry.getValue()+"\n");
for(Entry<String, Object> entry1:mapJsonObjectStrval1.entrySet()){
System.out.println("KEY:"+entry1.getKey()+" --> Value:"+entry1.getValue()+"\n");
}
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询