-2

This is my input

String str ="{\n" +
            "    \"myKey\": [{\n" +
            "            \"myHhome\": \"home1\",\n" +
            "            \"myAddress\": \"add\",\n" +
            "            \"dateTimeStamp\": \"Wed, 20 Mar 2019 14:38:54 GMT\"\n" +
            "        }\n" +
            "    ],\n" +
            "    \"basedata\": {\n" +
            "        \"mydata\": {\n" +
            "            \"mytype\": \"m1\",\n" +
            "            \"mytype2\": \"m2\"\n" +
            "        }\n" +
            "    }\n" +
            "}\n";

I chekced the json and it is valid I want to use GSON to get the value of the myHhome ( in my case hom1)

 static class Myclass{


    public String generation = null;


  }



 final Gson GSON1 = new Gson();
  String result= GSON1.fromJson(str,Myclass.class);
      System.out.println(result);

but i got null

Pshemo
  • 122,468
  • 25
  • 185
  • 269
user1365697
  • 5,819
  • 15
  • 60
  • 96
  • 1
    I really doubt you got null. What do you expect this code to do, precisely, and what does it do instead, precisely? Why did you create a MyClass class that has absolutely nothing to do with the structure of your JSON? – JB Nizet Mar 24 '19 at 12:11
  • I want to get the value of myHhome from the JSON – user1365697 Mar 24 '19 at 12:12
  • A vague question. You'll need to do some research(reading docs) on how GSON library works. – Mohamed Anees A Mar 24 '19 at 12:12
  • 1
    @user1365697 and do you really think that the code you posted will allow doing that? If yes, why do you think it should? If not, why don't you read the documentation of Gson to understand how it works and try something that makes sense? https://github.com/google/gson/blob/master/UserGuide.md – JB Nizet Mar 24 '19 at 12:14
  • I don't think we should -1 this. It's a valid question, however I don't think something has been done with it to be honest( which is OK, that's why SO community exists, to help them go with it ). – MS90 Mar 24 '19 at 12:16
  • 1
    "I want to..." that is not what others asked for. It is clear what you *want* from question, but what is not clear is *how* you approached it. We could show you *some* way to do it like examples shown in [How to parse JSON in Java](https://stackoverflow.com/q/2591098), but if you want to learn about mistake in *your* approach we must see valid [mcve] which will let us reproduce your problem. Currently you provided code which doesn't even compile since `GSON1.fromJson(str, Myclass.class)` doesn't return `String` but instance of `Myclass` which means it can't be run, so it can't give *any* value – Pshemo Mar 24 '19 at 12:39

3 Answers3

1

Don't understand what you trying to do.

As I understand MyClass should have getter and setter function Like

 public void setGeneration (String generation ) {
    this.generation = generation ;
}

public String getGeneration () {
    return generation ;
}

And call Gson

Gson gson = new GsonBuilder().create();
Myclass myclass= gson.fromJson(json, Myclass.class);
Ajoy
  • 189
  • 1
  • 5
  • 17
1

Your Json does not represents the class you are trying to deserialize it to.

{  
   "myKey":[  
      {  
         "myHhome":"home1",
         "myAddress":"add",
         "dateTimeStamp":"Wed, 20 Mar 2019 14:38:54 GMT"
      }
   ],
   "basedata":{  
      "mydata":{  
         "mytype":"m1",
         "mytype2":"m2"
      }
   }
}

Second, .fromJson is used to deserialization so the return must be the resultant Class.

Myclass myclass= GSON1.fromJson(json, Myclass.class);

For the Json you provided you need 2 more classes:

  • MyKey
  • MyData

    class Myclass {
    
      public List<MyKey> myKey = null;
      public Basedata basedata = null;
    
      // getters and setters
    }
    
    class Basedata { 
    
      private MyData mydata;
    
      // getters and setters
    }
    
    class MyData {
    
      private String mytype;
      private String mytype2;
    
      // getters and setters
    }
    
    class MyKey {
      public String myHhome;
      public String myAddress;
      public String dateTimeStamp;
    
      // getters and setters
    }
    

I hope it helps you to understand how classes are represented though Json.

1

If you only want to get the value of a json object, consider using a different library such as org.json or Jackson.

Using org.json:

JSONObject json = new JSONObject("your JSON string here"); // Parse the JSON
JSONArray array = json.getJSONArray("myKey");              // Get the JSON array represented by the key "myKey"
JSONObject home = array.getJSONObject(0);                  // Get the element at index 0 as a JSONObject
String result = home.getString("myHhome");                 // Get the string represented by the key "myHhome"
System.out.println(result);
Benjamin Urquhart
  • 1,626
  • 12
  • 18