zoukankan      html  css  js  c++  java
  • List和json数组的转换(赋源代码)

    public class a11111111 {
        
        //参数obj可以是 json对象,字符串, list
        public  static void fun(Object obj){
             JSONArray data = JSONArray.fromObject(obj);    
             for(int i=0;i<data.size();i++){
                  JSONObject jobj =  (JSONObject) data.get(i);
                  System.out.println(jobj);
                  /**
                   *{"aid":3201,"name":"品牌","cid":"1351"}
                    {"aid":7716,"name":"类型","cid":"1351"}
                    {"aid":8668,"name":"价格","cid":"1351"}
                   */        
                 String name = (String)jobj.get("name");
                 System.out.println(name); //  品牌  类型    价格
             } 
          } 
        
        
         public static void main(String[] args) { 
      //制作string
        
    //String list = request.getParameter("json"); String str9 = " [{"aid":3201,"name":"品牌","cid":"1351"}, {"aid":7716,"name":"类型","cid":"1351"}, {"aid":8668,"name":"价格","cid":"1351"}] " ; fun(str9); // 制作一个list List<Object> list90 = new ArrayList<Object>(); JSONArray data = JSONArray.fromObject(str9); for(int i=0;i<data.size();i++){ JSONObject jobj = (JSONObject) data.get(i); list90.add(jobj); // 将一个json数组转成list /** *{"aid":3201,"name":"品牌","cid":"1351"} {"aid":7716,"name":"类型","cid":"1351"} {"aid":8668,"name":"价格","cid":"1351"} */ } fun(list90); //调用的2个函数结果都是一样的,一样的 String str_0 = list90.toString(); fun(str_0); //结果都是一样的 } }
  • 相关阅读:
    sizeof运算符:c
    c语言学习笔记重点理解 杂 给自己看
    代码编写永远记住一句话
    xcode DEBUG每个按钮的解释说明 ios
    Consecutive Subsequence CodeForces
    Cyclic Components CodeForces
    nyoj-0737-石子合并(dp)
    nyoj-0708-ones(dp)
    nyoj-0613-免费馅饼(dp)
    nyoj-0469-擅长排列的小明 II(找规律)
  • 原文地址:https://www.cnblogs.com/cs-lcy/p/6985227.html
Copyright © 2011-2022 走看看