您的位置:首页 > Web前端 > JavaScript

关于list转换json

2017-02-03 14:21 232 查看
上传类似{"device_id":"8bae9c73330c3cee","user_id":"141","ios_lang_key":"en","person":[{"email":"201677577@qq.com","first_name":"nick","last_name":"nick","phone":"1"},
 {"email":"201677577@qq.com","first_name":"forrey","last_name":"forrey","phone":"1"},{"email":"201677577@qq.com","first_name":"forrey","last_name":"forrey","phone":"1"}]}的报文

 private void tojson() {

        List<Person> list = new ArrayList<Person>();

        Person p = new Person("201677577@qq.com","forrey","forrey","1");

        JSONObject js = new JSONObject();

        list.add(p);

        p = new Person("201677577@qq.com","nick","nick","2");

        list.add(p);

        p = new Person("201677577@qq.com","forrey","forrey","3");

        list.add(p);

        Gson gs = new Gson();

        JSONObject object=new JSONObject();

        JSONArray jsonArray=new JSONArray();

        try {

            for(int i = 0; i < list.size() - 1 ; i ++ )

        {

                js.put("email",list.get(i).getEmail());

                js.put("first_name",list.get(i).getFirst_name());

                js.put("last_name",list.get(i).getLast_name());

                js.put("phone",list.get(i).getPhone());

                jsonArray.put(js);

            }

            object.put("person", jsonArray);

          

        } catch (JSONException e) {

            e.printStackTrace();

        }

    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: