简体   繁体   English

使用Gson对JSON进行反序列化

[英]Deserialization of a JSON using Gson

I am receiving a JSON from a server that resembles the following syntax and I require some assistance deserializing & parsing it. 我正在从服务器接收类似于以下语法的JSON,我需要一些反序列化和解析的帮助。 I have done a lot of reading on this and have found that using GSON is really useful! 我对此进行了大量阅读,发现使用GSON非常有用! ( I Will post any updates to my code here) (我将在此处发布对我的代码的所有更新)

(Corrected JSON): (更正的JSON):

    [{
    "name" : "Zone1",
    "types" : [{"datatype":"string","brand":"string","index":0},
            {"datatype":"string","value":"int32,"index":1},
            {"datatype":"string","url":"string,"index":2}]
    "data" : [["gucci",2,"www.whoami12345.com"]]
   },
   {
   "name" : "Zone2",
   "types" : [{"datatype":"string","brand":"string","index":0},
            {"datatype":"string","value":"int32,"index":1},
            {"datatype":"string","url":"string,"index":2}]
   "data" : [["nike", 23,"www.nike.com"]]
  }]

I found this site Link pretty neat because it explains how to use gson and explains deserialisation well. 我发现该站点Link非常简洁,因为它解释了如何使用gson并很好地说明了反序列化。 My understanding of the JSON that I have is that it is an array and the data field is an array of Arrays. 我对JSON的理解是它是一个数组,而数据字段是一个Arrays数组。

My question is how do I go about parsing this? 我的问题是我该如何解析? I have a function that will take a string searching for a specific zone name. 我有一个函数,它将使用字符串搜索特定的区域名称。 After the deserialisation takes place and an entry matches the correct zone, the datatype and url are supposed to be returned. 反序列化发生并且条目匹配正确的区域后,应该返回数据类型和URL。 From that article, my understanding is that I should be using a JSONArray. 从那篇文章中,我的理解是我应该使用JSONArray。 Any feedback would be appreciated. 对于任何反馈,我们都表示感谢。 Below is some code of what I have started 下面是我已经开始的一些代码

import com.google.gson.JsonArray;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;

String name;    

public class data{
    String brand;
    int num;
    int url;
 }

public class types{
    String datatype;
    int value;
    String url;
}


public types Deserialiser(String json, String zone){ // this is the json string that will be passed into the function


JsonObject jsonObject = json.getAsJsonObject();
JsonArray jsonArray = jsonObject.getAsJsonArray();
int index = -1;
for (int i = 0; i<jsonArray.size();i++){
   String temp = jsonArray.get(i).get("name");
   if (temp.equals(zone){
      index =i;
      break;
   }

}

....

types jsonTypes = new types();
// set everything else
return jsonTypes;
}

Valid JSON (I think): 有效的JSON(我认为):

[{"name"  : "Zone1",
   "types" : ["datatype":"string","value":"int","url":"string"],
   "data"  : [["gucci",2,"www.whoami12345.com"]]},
  {"name"  : "Zone2",
   "types" : ["datatype":"string","value":"int","url":"string"],
   "data"  : [["nike", 23,"www.nike.com"]]}
]

Nope -- wrong missing "object" brackets 否-缺少“对象”括号

Try again: 再试一次:

[{"name"  : "Zone1",
   "types" : [{"datatype":"string","value":"int","url":"string"}],
   "data"  : [["gucci",2,"www.whoami12345.com"]]},
  {"name"  : "Zone2",
   "types" : [{"datatype":"string","value":"int","url":"string"}],
   "data"  : [["nike", 23,"www.nike.com"]]}
]

Ah!! 啊!! Much better! 好多了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM