zoukankan      html  css  js  c++  java
  • JAVA中如何将一个json形式的字符串转为json对象或对象列表

      

    import java.util.*;
    import java.text.SimpleDateFormat;
    
    import org.json.JSONObject;
    import org.json.JSONArray;
    import org.json.JSONException;
    
    
    
    public class test_client {
    
        public static  void test(){
            //把json字符串转为json对象
            String js ="{"uniqueCode":"OTTCD015","terminalBrandCode":"10","equipmentBrand":"夏普"}";
            JSONObject jsonObj = new JSONObject(js);
            System.out.println(jsonObj.get("uniqueCode"));
    
            //把json字符串列表转为json对象列表
            String jslist ="[{"uniqueCode":"OTTCD015","terminalBrandCode":"10","equipmentBrand":"夏普"}," +
                    "{"uniqueCode":"OTTCD016","terminalBrandCode":"30","equipmentBrand":"惠普"}]";
            JSONArray objar = new JSONArray(jslist);
            List<Object>  list = objar.toList();
            for(Object obj :list){
    //            System.out.println(obj.toString());
                Map<String,Object> map = (HashMap<String,Object>)obj;
                Set<String> sets = map.keySet();
                for(String key:sets){
                    System.out.println(key+": "+map.get(key));
                }
                System.out.println();
            }
        }

      输出:

        OTTCD015

        uniqueCode: OTTCD015
        terminalBrandCode: 10
        equipmentBrand: 夏普

        uniqueCode: OTTCD016
        terminalBrandCode: 30
        equipmentBrand: 惠普

  • 相关阅读:
    运算符
    javaScript注意事项
    初识JavaScript.
    tomee.xml
    setenv.bat
    catalina.properties
    tomee 系列问题
    tomee 第一个 远程调用 Message-driven bean(MDB)
    tomee 第一个 远程调用 Session Bean
    Java EE 参考文档及sample
  • 原文地址:https://www.cnblogs.com/shaosks/p/9617909.html
Copyright © 2011-2022 走看看