Jackson – Required property?

You can mark a property as required with the @JsonProperty(required = true) annotation, and it will throw a JsonMappingException during deserialization if the property is missing or null. Edit: I received a downvote for this without comment. I’d love to know why, since it does exactly the right thing.

Post a json body with swagger

You need to use the body parameter: “parameters”: [ { “in”: “body”, “name”: “body”, “description”: “Pet object that needs to be added to the store”, “required”: false, “schema”: { “$ref”: “#/definitions/Pet” } } ], and #/definitions/Pet is defined as a model: “Pet”: { “required”: [ “name”, “photoUrls” ], “properties”: { “id”: { “type”: “integer”, “format”: … Read more

How to populate a dropdownlist with json data in jQuery?

var listItems= “”; var jsonData = jsonObj.d; for (var i = 0; i < jsonData.Table.length; i++){ listItems+= “<option value=”” + jsonData.Table[i].stateid + “”>” + jsonData.Table[i].statename + “</option>”; } $(“#<%=DLState.ClientID%>”).html(listItems); Example <html> <head></head> <body> <select id=”DLState”> </select> </body> </html> /*javascript*/ var jsonList = {“Table” : [{“stateid” : “2”,”statename” : “Tamilnadu”}, {“stateid” : “3”,”statename” : “Karnataka”}, {“stateid” … Read more

How do I implement TypeAdapterFactory in Gson?

When you register a regular type adapter (GsonBuilder.registerTypeAdapter), it only generates a type adapter for THAT specific class. For example: public abstract class Animal { abstract void speak(); } public class Dog extends Animal { private final String speech = “woof”; public void speak() { System.out.println(speech); } } // in some gson related method gsonBuilder.registerTypeAdapter(Animal.class, … Read more

POST json to rails server

If you are sending in the right headers, then you won’t need to do “ActiveSupport::JSON.decode” — rails will do that for you. You’ll need to set the following headers in your post. Content-Type: application/json Accept: application/json A 422 means Unprocessable Entity — generally that there was a validation failure. You should be able to have. … Read more

How to return JSon object

First of all, there’s no such thing as a JSON object. What you’ve got in your question is a JavaScript object literal (see here for a great discussion on the difference). Here’s how you would go about serializing what you’ve got to JSON though: I would use an anonymous type filled with your results type: … Read more

How to receive JSON in a POST request in CherryPy?

Python import cherrypy class Root: @cherrypy.expose @cherrypy.tools.json_out() @cherrypy.tools.json_in() def my_route(self): result = {“operation”: “request”, “result”: “success”} input_json = cherrypy.request.json value = input_json[“my_key”] # Responses are serialized to JSON (because of the json_out decorator) return result JavaScript //assuming that you’re using jQuery var myObject = { “my_key”: “my_value” }; $.ajax({ type: “POST”, url: “my_route”, data: JSON.stringify(myObject), … Read more

Writing a json object to a text file in javascript

One thing you can do is setup the JSON as a download on the fly. var data = “{name: ‘Bob’, occupation: ‘Plumber’}”; var url=”data:text/json;charset=utf8,” + encodeURIComponent(data); window.open(url, ‘_blank’); window.focus(); Working demo: http://jsfiddle.net/sLq3F/ Apart from that, you can’t write a JSON to a file on the clientside due to security reasons. (Otherwise you have access to … Read more

How to map JSON to C# Objects

You can use Json.NET to deserialize your json string as (with some modifications to your classes) var yourObject = JsonConvert.DeserializeObject<Root>(jsonstring); public class Root { public Profile[] Profile; } public class Profile { public string Name { get; set; } public string Last { get; set; } public Client Client { get; set; } public DateTime … Read more