5
String jsons = "{'appname':'application', 'Version':'0.1.0', 'UUID':'300V', 'WWXY':'310W', 'ABCD':'270B', 'YUDE':'280T'}";

This is my json string. How can i parse it to GsonBuilder() that i will get object back? I try few thinks but none works.

I also read https://sites.google.com/site/gson/gson-user-guide

senzacionale
  • 20,448
  • 67
  • 204
  • 316

2 Answers2

19
public class YourObject {
   private String appname;
   private String Version;
   private String UUID;
   private String WWXY;
   private String ABCD;
   private String YUDE;
   //getters/setters

}  

parse to Object

YourObject parsed = new Gson().fromJson(jsons, YourObject.class);  

or

YourObject parsed = new GsonBuilder().create().fromJson(jsons, YourObject.class);  

minor test

String jsons = "{'appname':'application', 'Version':'0.1.0', 'UUID':'300V', 'WWXY':'310W', 'ABCD':'270B', 'YUDE':'280T'}";
YourObject parsed = new Gson().fromJson(jsons, YourObject.class);  

works well

EDIT
in this case use JsonParser

JsonObject object = new JsonParser().parse(jsons).getAsJsonObject();
object.get("appname"); // application 
object.get("Version"); // 0.1.0
Ilya
  • 29,135
  • 19
  • 110
  • 158
3

JSON uses double quotes ("), not single ones, for strings so the JSON you have there is invalid. That's likely the cause of any issues you're having converting it to an object.

Anthony Grist
  • 38,173
  • 8
  • 62
  • 76