GSON混合读 [英] gson with mixed read

查看:277
本文介绍了GSON混合读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图读取GSON JSON,但我不能让一个简单的例子GSON工作。

I'm trying to read json with gson, but I can't get a "simple" gson example to work.

来源: https://sites.google.com/site/gson/streaming

    public List<Message> readJsonStream(InputStream in) throws IOException {
      JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
      List<Message> messages = new ArrayList<Message>();
      reader.beginArray();
      while (reader.hasNext()) {
          Message message = gson.fromJson(reader, Message.class);
          messages.add(message);
      }
      reader.endArray();
      reader.close();
      return messages;
   }

这里的问题,如果我尝试:

Here's the problem, if I try with:

JsonReader reader;
Gson gson = new Gson();
gson.fromJson(reader,Program.class);

它甚至没有建立。

It doesn't even build.

The method fromJson(String, Class<T>) in the type Gson is not applicable for the arguments (JsonReader, Class<Program>)

有似乎是根据日食的方法:
fromJson(JsonReader为arg0,类型ARG1)

There seems to be a method according to eclipse: fromJson(JsonReader arg0, Type arg1)

推荐答案

替换

import android.util.JsonReader;   

import com.google.gson.stream.JsonReader 

做到了! =)

这篇关于GSON混合读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆