java - How to serialize json string using GSON and extract key and value from it? -


I am trying to use my JSON string using GSOn and Attract Key: add value to a simple Jason string and Load it map So far, I'm using org.json.simple.parser.JSONParser to do this. Now I am trying to use GSOn.

<">" Hello World "" hostname. ":" Abc1029.dc3.host "," "IPADDRESS": "100.671.1921.219", "hostid": "/ tt / rt / v2 / DC3 / 111", "FILE_TIMESTAMP": "Now I have to serial the above JSON string and with Jason String Need to remove it below the field and put it in it. "" "" Software "" "" "v13", "commit_hash": "abcdefg"}

key code pair In the map, it should mean that the description is in the map Code> key and in the form of the Hello World value, similarly for others.

  Public stable last set  ; MAPPING_FIELDS = New hashset & lt; string & gt; (Arrays.asList ("description", "hostname", "ipaddress", "hostid", "software_version")); personal last static JSONParser parser = new JSONParser (); ParseJSONData (jsonstringA, MAPPING_FIELDS); Public map & lt; string, string & gt; ParseJSONData (string jsonStr, set & lt; string & gt; listOfFields) {map & lt; String, string & gt; M = new Hashmap & lt; String, string & gt; (); Try {object obj = parser.parse (jsonStr); Org.json.simple.JSONObject jsonObject = (org.json.simple.JSONObject) Ob; If (jsonObject! = Null) {for (string field: listOfFields) {string value = (string) jsonObject.get (field); M.put (field, value); }}} Hold (ParseException e) {// entry exception here} return M; }  

How do I use GSOn here to do the same thing?

This is very simple, just a POJO Create a similar variable used in the JSON string.

Java Doc

View sample code:

  class ClientDetail {Private string description; Private string hostname; Private string ipadress; Private string hosted; Private string file_timestamp ;; Private string software_version; Private string command_hash; // Milan & amp; Setter} Buffer Reader = New Buffettder (new flamerdder (new file ("Jason .tttt")); Client Dictate Data = New GSN () FromJson (Reader, Client Dictate class);  

You can also convert it to map and convert it to the desired type.

Sample code:

  BufferedReader Reader = new BufferedReader (new FileReader (new file ("json.txt")); Type Type = New Typokan & lt; Maps & lt; String, string & gt; & Gt; () {} .getType (); Maps & lt; String, string & gt; Data = new gsn () FromJson (reader, type);  

Comments

Popular posts from this blog

Member with no value in F# -

java - Joda Time Interval Not returning what I expect -

c# - Showing a SelectedItem's Property -