「已注销」 2016-04-24 09:33 采纳率: 100%
浏览 1490
已采纳

MyJsonObject这个是什么,是类吗?

MyJsonObject代表什么,在下面这段代码里面。麻烦给看一下,谢谢

CloseableHttpClient httpclient = HttpClients.createDefault();

HttpGet httpget = new HttpGet("http://localhost/json");

    ResponseHandler<MyJsonObject> rh = new ResponseHandler<MyJsonObject>() {  

        @Override  
        public JsonObject handleResponse(  
                final HttpResponse response) throws IOException {  
            StatusLine statusLine = response.getStatusLine();  
            HttpEntity entity = response.getEntity();  
            if (statusLine.getStatusCode() >= 300) {  
                throw new HttpResponseException(  
                        statusLine.getStatusCode(),  
                        statusLine.getReasonPhrase());  
            }  
            if (entity == null) {  
                throw new ClientProtocolException("Response contains no content");  
            }  
            Gson gson = new GsonBuilder().create();  
            ContentType contentType = ContentType.getOrDefault(entity);  
            Charset charset = contentType.getCharset();  
            Reader reader = new InputStreamReader(entity.getContent(), charset);  
            return gson.fromJson(reader, MyJsonObject.class);  
        }  
    };  
    MyJsonObject myjson = client.execute(httpget, rh);  
  • 写回答

4条回答

  • erichk2008 2016-04-24 10:05
    关注

    从倒数第四行代码 :return gson.fromJson(reader, MyJsonObject.class); 可以判断MyJsonObject.class的性质

    Gson是google的一个Json库,使用非常简单。在Java中,只要引入包,创建对象就可以用了。

    fromJson是Gson提供的一个方法。用来将一个Json数据转换为对象。调用方法是:new Gson().fromJson(Json_string,class);

    这里的MyJsonObject.class应该是自定义的一个类,使用Gson就可以直接j将JSON数据封装成这个自定义的类直接使用,不用再手动解析Json里的每一行数据。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?