「已注销」 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条)

报告相同问题?

悬赏问题

  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏