I have searched the web for answer and everywhere is done like this:
JSONObject params = new JSONObject();
Gson gson = new Gson();
params.put("Event", gson.toJson(myEvent));
But this is returning me Json like this with all escapings on ":
"Event":"{\"Test1\":\"TestValue1\",\"TestObj\":{\"Test2\":\"TestValue2\",\"Test3\":\"TestValue3\"...
How can I have Json free of all escapings, just like this:
"Event":"{"Test1":"testValue1","TestObj":{"Test2":"testValue2","Test3":"testValue3"...
Cheers mates.
I can pass the object like this, property by property:
createEvent.put("Test1", myObj1.Test1);
createEvent.put("Test2", myObj2.Test2);
and pass the Json to api, but Notes is with escapings:
String myNoteListJson = new Gson().toJson(myNoteList);
createEvent.put("Notes", myNoteListJson);
I found an answer, here it is:
I am passing property by property:
createEvent.put("Test1", myObj1.Test1);
createEvent.put("Test2", myObj2.Test2);
and the Notelist problem is solved like this:
JSONArray mJSONArray = new JSONArray(myNoteList);
createEvent.put("Notes", mJSONArray);
Related
I have the following SP (SQL server) that return a Json output.
BEGIN
SET #jsonOutput = (
SELECT
Program.Name AS ProgramName,
ProgramOwner.FirstName AS OwnerFirstName,
FROM ProgramOwner, Program
WHERE Program.Id = ProgramOwner.ProgramOwner2Program
FOR JSON PATH,WITHOUT_ARRAY_WRAPPER)
I would like to map the return Json output to a List of ProgramDto via modelMapper. Not sure hot to do that since the return values from call.execute is an Object.
Something like this:
SimpleJdbcCall call = new
SimpleJdbcCall(jdbcTemplate).withProcedureName(programProc).declareParameters(
new SqlOutParameter("jsonOutput", Types.VARCHAR));
Map<String,Object>out = call.execute(new MapSqlParameterSource());
if(out.size()>0) {
// Only to show what I am trying to do
Type rootType = new TypeToken<List<ProgramDto>>() {}.getType();
modelMapper.map(out.get("jsonOutput"),rootType );
}
Thank you
As I understood you are trying to get a list of object from
You can use Jackson api
Like this
say for example your json is in variable named jsonData, then you can get the object you need like below.
ObjectMapper mapper = new ObjectMapper();
List<Type> myList = Arrays.asList(mapper.readValue(jsonData, Type[].class));
You can also find more examples here
After deserializing my string and converting it to JSON using the code below:
JSONObject returnValue = new JSONObject();
String toJson = null;
try
{
Object otherObjectValue = SerializationUtils
.deserialize(myBytesArray);
Gson gson = new Gson();
toJson = gson.toJson(otherObjectValue);
returnValue.put(key, toJson);
}
some part of the JSON still has something like:
{ "key":"ATTRIBUTE_LIST", "value":"{\"attributeContract\":[{\"scope\":\"sso\",\"name\":\"SAML_SUBJECT\",\"description\":\"Click to Edit\",\"required\":true}]}"}
which means everything in:
"{\"attributeContract\":[{\"scope\":\"sso\",\"name\":\"SAML_SUBJECT\",\"description\":\"Click to Edit\",\"required\":true}]}"
is one string instead being another object with fields. Is there something I can do to sanitize by JSONObject to make it properly JSON?
The key part is OK, means the whole String is JSON formatted.
For the value part, /shows that the value of value is JSON formatted already.
So you may "deserialize" the value of value again to retrieve an Object result. Or you may ask the creator of origin JSON, to serialize origin Object one time into JSON format.
My JSON Data looks something like this:
[
"{\"pid\":\"1\",\"title\":\"New CEO announced\",\"titleirish\":\"CEO nua\",\"content\":\"Bernard Byrne has been announced as the new CEO. Rejoice!\",\"contentirish\":\"Is Bernard Byrne an CEO. B\\\\u00edg\\\\u00ed s\\\\u00e1sta!\",\"imageurl\":\"http:\\\\\\/\\\\\\/scoiluiriada.ie\\\\\\/wp-content\\\\\\/uploads\\\\\\/2014\\\\\\/02\\\\\\/IMG_1781-150x112.jpg\",\"category\":\"News\",\"publishedby\":\"Andy\",\"modified\":\"2015-07-01 16:21:13\",\"buildings\":\"Bankcentre,Hume House,Time House\"}",
"{\"pid\":\"2\",\"title\":\"New CTO pronounced\",\"titleirish\":\"CEO nua\",\"content\":\"Bernard Byrne has been announced as the new CEO. Rejoice!\",\"contentirish\":\"Is Bernard Byrne an CEO. B\\\\u00edg\\\\u00ed s\\\\u00e1sta!\",\"imageurl\":\"http:\\\\\\/\\\\\\/scoiluiriada.ie\\\\\\/wp-content\\\\\\/uploads\\\\\\/2014\\\\\\/02\\\\\\/IMG_1781-150x112.jpg\",\"category\":\"News\",\"publishedby\":\"Andy\",\"modified\":\"2015-07-02 10:09:10\",\"buildings\":\"Hume House\"}",
....
So far I have the following code:
JSONParser parser = new JSONParser();
try {
Object obj = parser.parse(allData); // allData = JSON String above
JSONArray nitems = (JSONArray)obj;
This gives me an array of all the objects I want to parse. (it works perfectly)
Now, looping through this array, I would like to get the object member values.
Something like this:
nitems.get(0).getValueOf("title") // should return "New CEO announced"
nitems.get(0).getValueOf("titleirish") // "CEO Nua" etc.
This obviously doesn't work, what code do I use instead.
Thanks for any answers in advance.
Fixed with help from some comments. Here is solution:
JSONObject obj2 = (JSONObject)new JSONParser().parse(nitems.get(i).toString());
System.out.println(obj2.get("title").toString());
I am creating JSON object and send over the network , like
org.codehaus.jettison.json.JSONObject json = new org.codehaus.jettison.json.JSONObject();
json.put("id", "15");
json.put("code", "secret");
json.put("type", "new type");
Also I have links of photos what I want to put into this JSON
my links like http://box.com/images/photo.jpg,http://box.com/images/photo1.jpg
http://box.com/images/photo2.jpg, http://box.com/images/photo3.jpg
As I understand I must have some list/array and put like
json.put("images", links)
How to do it, put and parse... I need one key, and list of values.
Is JSON array is useful for this?
Thanks
Yes. JSONArray is what you need.
List <String> links = getLinks();
JSONArray array = new JSONArray();
for (String link : links)
array.put(link);
JSONObject obj = new JSONObject();
//put id, code, type...
obj.put("images", array);
Check out the JSONArray class.
http://jettison.codehaus.org/apidocs/org/codehaus/jettison/json/JSONArray.html
You'll create a JSONArray and use that in your put command.
I have a JSON response which looks like this:
String resp = "{"name":"Renold","age":"16"}"
And I have a POJO named "Person" which contains the attributes 'Name' and 'Age'. How do I extract the attributes from the JSON response and assign them to the POJO? I have already tried GSON and the object gets incorrectly assigned as "Name = name" and "Age = age" instead of the actual response.
Any other suggestions, good lads? :)
EDIT: This is what I used with GSON
Gson gson = new Gson();
final Person p= gson.fromJson(response, Person.class);
This left me with:
p.Name = name;
p.Age = age;
instead of p.Name = Renold.
You can try this with Genson:
String resp = "{"name":"Renold","age":"16"}"
Genson genson = new Genson();
Person person = genson.deserialize(resp, Person.class);
I guess you have to use #SerializedName to map Name to name
check this pojo parse gson with invalid java names
In your Pojo you have to annotate like this.
#SerializedName("name")
private final Name;
Please try serialization using the JObject class using org.json. e.g
JSONObject jsonObj = new JSONObject("{\"name\":\"Renold\",\"age\":\"16"\"}");
Then get the properties:
jsonObj.get("name");
If you're using maven you can include the Jackson dependency. That will map things automagically.
I have used the same scenario you have described and found no issues with your code except the String representation you have used. I have put my code at pastebin. This sample project should resolve your issue of json format.
The string in your response should be:
String response = "{\"name\":\"Renold\",\"age\":16}"