Active5 months ago
- Java Serialize Object To String File
- Deserialize Json To Object Java
- Java Serialize Object To String And Back
Going From a String to an Object. JSON.parse parses a string as JSON, so it will take in a string value and output a JSON value. Ergo, it’s the opposite of JSON.stringify. That’s enough stringification fun for now. Manipulating JSON into your string format is pretty easy to do, but as always, if you’re still stuck, drop us a line below.
I need to serialize some objects to a JSON and send to a WebService. How can I do it using the org.json library? Or I'll have to use another one? Here is the class I need to serialize:
I only put the variables and the constructor of the class but it also have the getters and setters. So if anyone can help please
AurA10.2k55 gold badges4141 silver badges5757 bronze badges
Tiago GeaneziniTiago Geanezini34211 gold badge44 silver badges1414 bronze badges
7 Answers
Easy way to do it without annotations is to use Gson library
Simple as that:
Tombart20.5k1111 gold badges9898 silver badges107107 bronze badges
BitmanBitman
The quickest and easiest way I've found to Json-ify POJOs is to use the Gson library. This blog post gives a quick overview of using the library.
ryanbatemanryanbateman
You make the http request
You read the Buffer
Create a JSONObject and pass the result string to the constructor:
Parse the json results to your desired variables:
Do not forget to import:
Carlos LanderasCarlos Landeras9,6601111 gold badges4545 silver badges7777 bronze badges
GSON is easy to use and has relatively small memory footprint. If you loke to have even smaller footprint, you can grab:
Which is tiny wrapper around stripped down GSON libraries for just POJOs
Konstantin PribludaKonstantin Pribluda11.2k11 gold badge2323 silver badges3434 bronze badges
![String String](/uploads/1/2/4/8/124892911/632103601.jpg)
The 'reference' Java implementation by Sean Leary is here on github. Make sure to have the latest version - different libraries pull in versions buggy old versions from 2009.
Java EE 7 has a JSON API in
javax.json
, see the Javadoc. From what I can tell, it doesn't have a simple method to marshall any object to JSON, you need to construct a JsonObject
or a JsonArray
.But I assume the other libraries like GSON will have adapters to create objects implementing those interfaces.
Ondra ŽižkaOndra Žižka22.2k2828 gold badges153153 silver badges223223 bronze badges
One can use the Jackson library as well.
![Object Object](/uploads/1/2/4/8/124892911/759199103.jpg)
Simply do this:
nasprasnaspras58422 gold badges1515 silver badges3737 bronze badges
After JAVAEE8 published , now you can use the new JAVAEE API JSON-B (JSR367)
Maven dependency :
Here is some code snapshot :
JSON-P is also updated to 1.1 and more easy to use. JSON-P 1.1 (JSR374)
Maven dependency :
Here is the runnable code snapshot :
wodongwodong
Java Serialize Object To String File
Not the answer you're looking for? Browse other questions tagged javaandroidjson or ask your own question.
Active8 years, 4 months ago
this may be a stupid question but I wanted to convert a 2 dimensional string array into a serializable object in java. What is the best way to do this?
DaveyDavey
2 Answers
Arrays are already Serializable. So is String. You don't need anything more.
Here is a complete example :
Resources:
user207421270k2828 gold badges231231 silver badges385385 bronze badges
Colin HebertDeserialize Json To Object Java
Colin Hebert78.6k1212 gold badges143143 silver badges139139 bronze badges
Here goes the answer. Arrays are by default serializable. Just write it to ObjectOutputStream like another serializable object
Ramesh PVKRamesh PVKJava Serialize Object To String And Back
13.9k11 gold badge3939 silver badges4949 bronze badges