Naresh
Naresh

Reputation: 81

Parse json in java for a particular format

I'm new to java. Could you please help me in parsing the below json in java. I have a json which contains a list of empid's and for each empid there will be couple of dept id's.

Please see the below format

empid  deptid
1       111
1       222
1       333
2       123
2       111

So I will have the data in json as above format. And I need parse this json in java and I need to store this data into a map.

Like Map<String,List<String>> where key will be the empid and the value will be dept id's.

So please help me with this.

Note : I will get input as Json Object from client. I need to parse this json object in java.

Json Representation :

  {"employees":[
   {"empid":"1","deptid":["111","222","333"]},
   {"empid":"2","deptid":["123","111"]},
]}

Thanks.

Upvotes: 1

Views: 109

Answers (1)

Mangoose
Mangoose

Reputation: 922

Use gson library https://github.com/google/gson

use typetoken to parse json string to java object

Gson gson = new Gson();
Type type = new TypeToken<Map<String,List<String>>>(){}.getType();
Map<String,List<String>> obj = gson.fromJson(jsonData, type );

Upvotes: 1

Related Questions