-3

I have the following JSON string.

{"portfolio":"HEXGENFUND","transSrlNo":"1","transCode":"BUY","investReason":"009","inflowOutflow":"I","transDate":"2012-09-01","tradeDate":"2012-09-01","tradeDateUpto":"2012-09-01","tradeTime":"15:46:36","investCategory":"FVTPL","custodian":"DEUTSCHE","holdType":"HOLD","securityType":"INV","security":"9.45SBAI160326","assetClass":"NCD","issuer":"SBAI","marketType":"LIM","tradePriceType":"R","requisitionType":"MS","priceFrom":"343490934332","priceTo":"343490934332","marketPrice":"343490934332","averagePrice":"343490934332","price":"343490934332","quantity":"234","grossAmtTcy":"80376878633688","exchRate":"1","grossAmtPcy":80376878633688,"grossIntTcy":"992.42","grossIntPcy":992.42,"netAmountTcy":80376878634680.42,"netAmountPcy":80376878634680.42,"acquCostTcy":80376878633688,"acquCostPcy":80376878633688,"yieldType":"N","purchaseYield":"0","marketYield":"0","ytm":"-80.07453968","mduration":"0","currPerNav":"0","desiredPerNav":"0","currHolding":"0","noofDays":"0","realGlPcy":0,"realGlTcy":"0","nowLater":"N","isAllocable":"false","acquCostReval":0,"acquCostHisTcy":80376878633688,"acquCostHisPcy":0,"exIntTcy":0,"exIntPcy":0,"accrIntReval":0,"accrIntTcy":0,"accrIntPcy":0,"grossAodTcy":0,"grossAodPcy":0,"grossAodReval":0,"bankAccAmtAcy":80376878634680.42,"bankAccAmtPcy":80376878634680.42,"taxAmountTcy":0,"unrelAmortTcy":0,"unrelAmortPcy":0,"unrelGlTcy":0,"unrelGlPcy":0,"realGlHisTcy":0,"realGlHisPcy":0,"tradeFeesTcy":0,"tradeFeesPcy":0}

I have a POJO for the above.

How do I deserialize this to a Object?

stevedbrown
  • 8,862
  • 8
  • 43
  • 58
Java Questions
  • 7,813
  • 41
  • 118
  • 176

5 Answers5

11

You can use gson library like this to convert above JSON string to HashMap:

Gson gson = new Gson(); // get a new Gson instance

// define a Type for Map<String, String>
Type type = new TypeToken<Map<String, String>>(){}.getType();

// convert JSON string to a Map<String, String> instance
Map<String, String> map = gson.fromJson(jsonStr, type);

EDIT (Based your Edited question): For converting JSON to your POJO object

You can convert JSON string to your custom POJO using gson like this:

// assuming your POJO is of type MyClass
MyClass instance = gson.fromJson(jsonStr, MyClass.class);
anubhava
  • 761,203
  • 64
  • 569
  • 643
3

If you want to generate the matching java class for the above json string, going with a json generator would be the best solution since the data has many fields.

You may check http://www.jsonschema2pojo.org/

Just paste your json, select JSON as source type and you will have a Jackson compatible annotated java class.

Serkan Arıkuşu
  • 5,549
  • 5
  • 33
  • 50
3

Thake a look at this library tutorial: jackson

String myJson = new org.codehaus.jackson.map.ObjectMapper.writeValueAsString(myObject);

Alepac
  • 1,833
  • 13
  • 24
2

There are some JSON libraries that may helps you. For example, Gson is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object

Miguel Prz
  • 13,718
  • 29
  • 42
2

You can create JSON object by using libraries like gson or JsonOrg. You can take a lokk at the following links for libraries and implementations

https://sites.google.com/site/gson/gson-user-guide
http://code.google.com/p/google-gson/
http://json.org/java/
karthick
  • 11,998
  • 6
  • 56
  • 88