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

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

本文介绍了如何解析JSON解析在Android中使用GSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 GSON 的解析JSON数据。我的JSON数据是如下:

  {
    伯爵:12,
    colbreak:1,
    名:一元RELS
    得分:9090,
    言:[
        {
            伯爵:6,
            字:prp_għaċ-
            名:prp_għaċ-
            分数:9.1,
            言:科尔
            求:2231297
        }
    ]
    求:0
}

GsonParse.java

 公共类GsonParse {
 @SerializedName(计数)
 公共字符串计数; @SerializedName(colbreak)
 公共字符串colbreak; @SerializedName(名称)
 公共字符串计数;
 @SerializedName(分数)
 公共字符串分; @SerializedName(言)
 公开名单<单词GT; mWords =新的ArrayList<单词GT;(); @SerializedName(追求)
 公共字符串求;
}

我用下面的方法来解析这个JSON数据。

 公共静态< T> ArrayList的< T> JsonParse(T(T),字符串响应){
        //字符串转换成的InputStream
        在的InputStream =新ByteArrayInputStream进行(response.getBytes());
        JsonReader阅读器;
        ArrayList的< T> LCS =新的ArrayList< T>();
        尝试{
            读者=新JsonReader(新InputStreamReader的(在UTF-8));
            GSON GSON =新GSON();            reader.beginObject();
            而(reader.hasNext()){                ŧCSE =(T)gson.fromJson(读卡器,t.getClass());
                lcs.add(CSE);
            }            reader.endObject();            / *
             * reader.nextName(); reader.nextString(); reader.nextName();
             * reader.nextString();
             * /
            reader.close();
        }赶上(UnsupportedEncodingException五){
            // TODO自动生成catch块
            e.printStackTrace();
        }赶上(IOException异常五){
            // TODO自动生成catch块
            e.printStackTrace();
        }        返回(ArrayList的< T>)LCS;
    }

我面对下面的错误。

  10月3日至31日:14:26.968:E / AndroidRuntime(18578):com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期BEGIN_OBJECT但名称第1行列73


解决方案

您可以尝试读取GSON值是这样的:

  {尝试
      AssetManager assetManager = getAssets();
  InputStream的IMS = assetManager.open(file.txt的);  GSON GSON =新GSON();
  读卡器读卡器=新的InputStreamReader(IMS);  GsonParse gsonObj = gson.fromJson(读卡器,GsonParse.class);     }赶上(IOException异常五){
       e.printStackTrace();
 }

假设你只是收到此一个街区,而不是一个列表。而且这个数据目前在资产文件夹中的文件。你可以将其更改为你想从阅读数据流。

您使用应该像类:

GsonParse.class

 公共类GsonParse {
 @SerializedName(计数)
 私人字符串计数; @SerializedName(colbreak)
 私人字符串colbreak; @SerializedName(名称)
 私人字符串名称; @SerializedName(分数)
 私人字符串分; @SerializedName(言)
 私人列表<单词GT; mWords =新的ArrayList<单词GT;(); @SerializedName(追求)
 私人字符串求;公共字符串的getCount(){
    返回计数;
}公共无效setCount(字符串计数){
    this.count =计数;
}公共字符串getColbreak(){
    返回colbreak;
}公共无效setColbreak(字符串colbreak){
    this.colbreak = colbreak;
}私人字符串的getName(){
    返回名称;
}私人无效setname可以(字符串名称){
    this.name =名称;
}公共字符串getScore(){
    返回分数;
}公共无效setScore(字符串分数){
    this.score =得分;
}公开名单<单词GT; getmWords(){
    返回mWords;
}公共无效setmWords(列表<单词GT; mWords){
    this.mWords = mWords;
}公共字符串getSeek(){
    返回求;
}公共无效setSeek(字符串搜索){
    this.seek =求;
}
}

Words.class

 公共类词{
@SerializedName(值=计数)
私人字符串计数;
@SerializedName(值=字)
私人字符串字;
@SerializedName(值=得分)
私人字符串名称;
@SerializedName(值=言)
私人字符串的话;
@SerializedName(值=求)
私人字符串求;    公共字符串的getCount(){
    返回计数;
}
公共无效setCount(字符串计数){
    this.count =计数;
}
公共字符串屏幕取词(){
    返回字;
}
公共无效setWord(字符串字){
    this.word =字;
}
公共字符串的getName(){
    返回名称;
}
公共无效setname可以(字符串名称){
    this.name =名称;
}
公共字符串getWords(){
    返回的话;
}
公共无效setWords(字符串字){
    this.words =字;
}
公共字符串getSeek(){
    返回求;
}
公共无效setSeek(字符串搜索){
    this.seek =求;
}
}

有一个参数words.class丢失,您可以添加它。

GSON并不直接支持UTF-8字符。所以使用http接收到响应的时候,你必须将其转换为UTF-8格式的HTTP本身的响应。

您可以尝试使用:

 字符串jsonString =新GSON()的toJSON(objectToEn code)。
字节[] = utf8JsonString jsonString.getBytes(UTF8);
responseToClient.write(utf8JsonString,0,utf8JsonString.Length);

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

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

I am facing below error.

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
解决方案

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

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

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

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.

you could try using:

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

这篇关于如何解析JSON解析在Android中使用GSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 11:42