如何在android中使用GSON解析json解析

2022-01-31 00:00:00 json android java gson

我正在使用 GSON 来解析 Json 数据.我的 Json 数据如下:

I am using GSON for parse Json data. My Json data is below:

{
    "count": "12",
    "colbreak": 1,
    "name": "unary rels",
    "score": "9090",
    "Words": [
        {
            "count": 6,
            "word": "prp_għaċ-",
            "name": "prp_għaċ-",
            "score": 9.1,
            "Words": "kol",
            "seek": 2231297
        }
    ],
    "seek": 0
}

GsonParse.java

public class GsonParse {


 @SerializedName("count")
 public String count;

 @SerializedName("colbreak")
 public String colbreak;

 @SerializedName("name")
 public String count;
 @SerializedName("score")
 public String score;

 @SerializedName("Words")
 public List<Words> mWords = new ArrayList<Words>();

 @SerializedName("seek")
 public String seek;
}

我正在使用下面的方法来解析这个 JSON 数据.

I am using below method to parse this JSON data.

public static <T> ArrayList<T> JsonParse(T t, String response) {
        // convert String into InputStream
        InputStream in = new ByteArrayInputStream(response.getBytes());
        JsonReader reader;
        ArrayList<T> lcs = new ArrayList<T>();
        try {
            reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
            Gson gson = new Gson();

            reader.beginObject();
            while (reader.hasNext()) {

                T cse = (T) gson.fromJson(reader, t.getClass());
                lcs.add(cse);
            }

            reader.endObject();

            /*
             * reader.nextName(); reader.nextString(); reader.nextName();
             * reader.nextString();
             */
            reader.close();
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return (ArrayList<T>) lcs;
    }

我面临以下错误.

03-31 10:14:26.968: E/AndroidRuntime(18578): com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was NAME at line 1 column 73

推荐答案

你可以试试这样读取 gson 值:

you could try reading the gson value like this:

try {
      AssetManager assetManager = getAssets();
  InputStream ims = assetManager.open("file.txt");

  Gson gson = new Gson();
  Reader reader = new InputStreamReader(ims);

  GsonParse gsonObj = gson.fromJson(reader, GsonParse.class);

     }catch(IOException e) {
       e.printStackTrace();
 }

假设您只是收到这一块而不是列表.而且这些数据当前位于 assets 文件夹中的一个文件中.您可以将其更改为要从中读取的流.

Assuming that you are just receiving this one block and not a list. And also this data is currently in a file in the assets folder. You can change it to the stream you want to read it from.

你使用的类应该是这样的:

The class you use should look like:

GsonParse.class

public class GsonParse {
 @SerializedName("count")
 private String count;

 @SerializedName("colbreak")
 private String colbreak;

 @SerializedName("name")
 private String name;

 @SerializedName("score")
 private String score;

 @SerializedName("Words")
 private List<Words> mWords = new ArrayList<Words>();

 @SerializedName("seek")
 private String seek;

public String getCount() {
    return count;
}

public void setCount(String count) {
    this.count = count;
}

public String getColbreak() {
    return colbreak;
}

public void setColbreak(String colbreak) {
    this.colbreak = colbreak;
}

private String getName() {
    return name;
}

private void setName(String name) {
    this.name = name;
}

public String getScore() {
    return score;
}

public void setScore(String score) {
    this.score = score;
}

public List<Words> getmWords() {
    return mWords;
}

public void setmWords(List<Words> mWords) {
    this.mWords = mWords;
}

public String getSeek() {
    return seek;
}

public void setSeek(String seek) {
    this.seek = seek;
}
}

Words.class

public class Words {
@SerializedName(value ="count")
private String count;
@SerializedName(value="word")
private String word;
@SerializedName(value="score")
private String name;
@SerializedName(value="Words")
private String words;
@SerializedName(value="seek")
private String seek;

    public String getCount() {
    return count;
}
public void setCount(String count) {
    this.count = count;
}
public String getWord() {
    return word;
}
public void setWord(String word) {
    this.word = word;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getWords() {
    return words;
}
public void setWords(String words) {
    this.words = words;
}
public String getSeek() {
    return seek;
}
public void setSeek(String seek) {
    this.seek = seek;
}
}

words.class中缺少一个参数,你可以添加它.

there is a parameter missing in words.class, you could add it .

GSON 不直接支持 UTF-8 字符.所以当使用 http 接收响应时,您必须在 http 本身的响应中将其转换为 utf-8 格式.

GSON does not directly support UTF-8 characters. so when receiving the response using http, you will have to convert that to utf-8 form in the response of http itself.

您可以尝试使用:

String jsonString = new Gson().toJson(objectToEncode);
byte[] utf8JsonString = jsonString.getBytes("UTF8");
responseToClient.write(utf8JsonString, 0, utf8JsonString.Length);

相关文章