简体   繁体   中英

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. I have done a lot of reading on this and have found that using GSON is really useful! ( I Will post any updates to my code here)

(Corrected 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. My understanding of the JSON that I have is that it is an array and the data field is an array of 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. From that article, my understanding is that I should be using a 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):

[{"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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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