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

ilmyapht  于 2022-11-06  发布在  Android
关注(0)|答案(5)|浏览(186)

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

{
    "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数据。

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
kninwzqo

kninwzqo1#

您可以尝试阅读gson值,如下所示:

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文件夹的一个文件中。你可以把它改到你想从中读取它的流中。
您使用的类应如下所示:

GsonParse.类别

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;
}
}

单词.类

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中缺少一个参数,您可以添加它。
GSON不直接支持UTF-8字符,所以当使用http接收响应时,您必须在http本身的响应中将其转换为UTF-8格式。
您可以尝试使用:

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

umuewwlo2#

您好使用以下等级库

compile 'com.google.code.gson:gson:2.2.4'

Json类别

import java.util.List;
public class GsonParse{

/**
 * 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
 */

private String count;
private int colbreak;
private String name;
private String score;
private int seek;
/**
 * count : 6
 * word : prp_għaċ-
 * name : prp_għaċ-
 * score : 9.1
 * Words : kol
 * seek : 2231297
 */

private List<WordsBean> Words;

public String getCount() {
    return count;
}

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

public int getColbreak() {
    return colbreak;
}

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

public String getName() {
    return name;
}

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

public String getScore() {
    return score;
}

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

public int getSeek() {
    return seek;
}

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

public List<WordsBean> getWords() {
    return Words;
}

public void setWords(List<WordsBean> Words) {
    this.Words = Words;
}

public static class WordsBean {
    private int count;
    private String word;
    private String name;
    private double score;
    private String Words;
    private int seek;

    public int getCount() {
        return count;
    }

    public void setCount(int 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 double getScore() {
        return score;
    }

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

    public String getWords() {
        return Words;
    }

    public void setWords(String Words) {
        this.Words = Words;
    }

    public int getSeek() {
        return seek;
    }

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

}
调用API将响应放入以下代码并检索数据

GsonParse gsonparse = gson.fromJson(response, GsonParse.class);
//gsonparse.getWords() // It will returns list of Words
//Also do loop and get more data using data
gsonparse.getColbreak();
gsonparse.getSeek();
for (GsonParse.WordsBean data:gsonparse.getWords())
{

    data.getName();

}

我希望这对你有帮助。

lf3rwulv

lf3rwulv3#

替换

@SerializedName("name")
public String count;

@SerializedName("name")
public String name;

我猜问题是您将public String count;同时用于@SerializedName("count")@SerializedName("name")

  • 谢谢-谢谢
uinbv5nw

uinbv5nw4#

试试看:

JSONArray jsonarray = jsonObject.getJSONArray("responseData");
Type listType = new TypeToken<ArrayList<AllUsers>>(){}.getType();
List<AllUsers> allUserses = 
    new GsonBuilder().create().fromJson(jsonarray.toString(), listType);

for(AllUsers user: allUserses){
         allUsersDao.insertOrReplace(user);
}
7qhs6swi

7qhs6swi5#

您的代码中缺少许多内容。
请看下面的代码并尝试此代码。

import java.util.List;

public class GsonParse {

    public GsonParse() {
    }

    public String count = "";
    public String colbreak = "";
    public String name = "";
    public String score = "";
    public int seek = 0;
    List<WordsData> Words;

    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;
    }

    public String getName() {
        return name;
    }

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

    public String getScore() {
        return score;
    }

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

    public int getSeek() {
        return seek;
    }

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

    public List<WordsData> getWords() {
        return Words;
    }

    public void setWords(List<WordsData> words) {
        Words = words;
    }

    public class WordsData {

        public WordsData() {
        }

        public int count = 0;
        public String word = "";
        public String name = "";
        public double score = 0.0;
        public String Words = "";
        public int seek = 0;

        public int getCount() {
            return count;
        }

        public void setCount(int 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 double getScore() {
            return score;
        }

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

        public String getWords() {
            return Words;
        }

        public void setWords(String words) {
            Words = words;
        }

        public int getSeek() {
            return seek;
        }

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

}

有关更多详细信息,请查看此example

相关问题