把字符串转换成Json格式

时间:2025-03-28 16:07:57

需要用到第三方jar包,添加依赖

compile ':gson:2.7'


  @Override
    protected void onCreate(Bundle savedInstanceState) {
        (savedInstanceState);
        setContentView(.activity_main);
        Gson gson = new Gson();
        String s = (new ToJosn( "duan", "hubei"));

    }

    class ToJosn {
        public String name;
        public String city;

        public ToJosn( String name, String city) {
             = name;
             = city;
        }
    }


这样s就是转换成json格式的数据了