Answers for "how to convert java object to json"

41

convert object to json javascript

var person={"first_name":"Tony","last_name":"Hawk","age":31};
var personJSONString=JSON.stringify(person);
Posted by: Guest on July-23-2019
2

how to convert jsonobject to json string in java

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class TestJSON {

	public static void main(String[] args) {

		JSONObject jObject = new JSONObject();

		jObject.put("EmployeeId", new Integer(121));
		jObject.put("Name", "Ramesh");
		jObject.put("Salary", new Double(15000.00));
		jObject.put("isPermanent", new Boolean(true));
		jObject.put("Nickname", null);
		
		//convert from JSONObject to JSON string
		String jsonText = jObject.toJSONString();

		System.out.println(jsonText);

		JSONParser parser = new JSONParser();
		
		//convert from JSON string to JSONObject
		JSONObject newJObject = null;
		try {
			newJObject = (JSONObject) parser.parse(jsonText);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		System.out.println(newJObject.get("EmployeeId"));
		System.out.println(newJObject.get("Name"));
		System.out.println(newJObject.get("Salary"));
		System.out.println(newJObject.get("isPermanent"));
		System.out.println(newJObject.get("Nickname"));
	}
}
Posted by: Guest on December-25-2020
0

convert json string to json object in java

try {
     JSONObject jsonObject = new JSONObject("{\"phonetype\":\"N95\",\"cat\":\"WP\"}");
}catch (JSONException err){
     Log.d("Error", err.toString());
}
Posted by: Guest on September-29-2020
-1

How To Convert Json To Java Object

I add GSON dependency in my POM --> It’s a json parser. That is
used to convert from java object to json and from json to java
object
SERIALIZATION: CONVERT JAVA OBJECT -> JSON
DE-SERIALIZATION: CONVERT JSON -> JAVA OBJECT
Posted by: Guest on December-04-2020

Code answers related to "how to convert java object to json"

Code answers related to "Javascript"

Browse Popular Code Answers by Language