zoukankan      html  css  js  c++  java
  • java_json 转换 文件在file中

    import java.util.ArrayList;
    import java.util.List;

    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;

    class Dog {
        private int id;
        private String name;

        public Dog(int id, String name) {
            super();
            this.id = id;
            this.name = name;
        }

        public Dog() {
            super();
            // TODO Auto-generated constructor stub
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

    }

    public class D {

        /**
         * @param args
         * @throws JSONException
         */
        public static void main(String[] args) throws JSONException {
            // TODO Auto-generated method stub
            // JSONArray a = new JSONArray();

            // 将一个字符串转化成一个json字符串
            JSONObject o = new JSONObject();
            o.put("name", "tom");
            System.out.println(o.toString());

            //
            List<Dog> a = new ArrayList<Dog>();
            a.add(new Dog(1,"tom1"));
            a.add(new Dog(2,"tom2"));
            a.add(new Dog(3,"tom3"));
            a.add(new Dog(4,"tom4"));
            //JSONArray aa = new JSONArray(a);
            //System.out.println(aa.toString());
            
            JSONArray arr = new JSONArray();
            JSONObject oo;
            for(Dog user : a){
            o = new JSONObject();
            o.put("id", user.getId());
            o.put("name", user.getName());
            arr.put(o);
            }
            System.out.println(arr.toString());

        }

    }
  • 相关阅读:
    2015.05.12:json的常用处理方式
    好菜都得花功夫做
    网站从 IIS6 迁移至 IIS7 后的通配符脚本映射权限配置不当可能导致403错误
    ELearning是学习系统而不是教育系统
    空难与软件开发(一)
    “模态”对话框和“后退”按钮
    闭包解析(Fun with closure)
    关于CultureInfo的两个问题
    从来就不可能精通:关于Boxing
    Windows RT 应用程序开发介绍培训的讲义
  • 原文地址:https://www.cnblogs.com/kaka100/p/2615755.html
Copyright © 2011-2022 走看看