JAVA中如何将⼀个json形式的字符串转为json对象或对象列表
import java.util.*;
SimpleDateFormat;
import org.json.JSONObject;
import org.json.JSONArray;
import org.json.JSONException;
json转换对象public class test_client {
public static void test(){
//把json字符串转为json对象
String js ="{\"uniqueCode\":\"OTTCD015\",\"terminalBrandCode\":\"10\",\"equipmentBrand\":\"夏普\"}";
JSONObject jsonObj = new JSONObject(js);
System.out.("uniqueCode"));
//把json字符串列表转为json对象列表
String jslist ="[{\"uniqueCode\":\"OTTCD015\",\"terminalBrandCode\":\"10\",\"equipmentBrand\":\"夏普\"}," +
"{\"uniqueCode\":\"OTTCD016\",\"terminalBrandCode\":\"30\",\"equipmentBrand\":\"惠普\"}]";
JSONArray objar = new JSONArray(jslist);
List<Object> list = List();
for(Object obj :list){
// System.out.String());
Map<String,Object> map = (HashMap<String,Object>)obj;
Set<String> sets = map.keySet();
for(String key:sets){
System.out.println(key+": "+(key));
}
System.out.println();
}
}
输出:
OTTCD015
uniqueCode: OTTCD015
terminalBrandCode: 10
equipmentBrand: 夏普
uniqueCode: OTTCD016
terminalBrandCode: 30
equipmentBrand: 惠普
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论