I would like to validate incoming json object for correctness at the server side. Is there a standard / optimal way to do that? What is your approach of validation?
My advice - deserialize the JSON and see if it breaks. For example, if you're using C# on the server side, you can use the newfangled DataContractJsonSerializer, or do it the old way with the JavaScriptSerializer which is arguably much simpler.
var serializer = new JavaScriptSerializer();
var result = serializer.Deserialize<Dictionary<string, object>>(jsonString);
EDIT: And now that it's come out that you're using Java, of course my C# example is not going to work for you, but the concept is the same. Stackoverflow already has some answers here: Convert a JSON string to object in Java ME?
Decode it with a JSON library. If it successfully decodes using a library that follows the specifications then it's valid.
Related
I'm working on a springboot project and having some trouble with ElasticSearch.
The user will put some JSON-format elasticsearch DSL query strings in the database and they are black-box to me. What I need to do is get the query strings and use them so search information in elasticsearch.
In python, the DSL can be a parameter like this:
body = {
"query":{
"match_all":{}
}
}
es.search(index="my_index",doc_type="test_type",body=body)
How can I perform the search without knowing the details of the string and just using the JSON format query in Java?
I believe there are two ways to do it in modern ES client libraries. I haven't tried them myself, but first one seems to be pretty straightforward.
First one is using low-level client:
Request request = new Request("POST", "/index/_search");
request.setJsonEntity(jsonString);
Response response = client.performRequest(request);
Seems like it's enough just to push JSON as string into setJsonEntity, and you're already set.
Second one is to use high level client, and this gets tricky, though it can provide more robust API. As you might know, elasticsearch has concept of XContent, which is serialization/deserialization to/from different formats, including JSON. Theoretically, it is possible to create JsonXContentParser, which then can be used to instantiate SearchSourceBuilder:
SearchSourceBuilder.fromXContent(jsonXContentParser);
The problem is only that JsonXContentParser requires number of arguments to be instantiated, and i'm not sure how to properly create those dependencies.
I would like to extract only the data present in a Json to JsonForm using Java.
Which framework can I use to this operation?
I've found the answer. Don't let the ugly aspect of JsonForm deceive you. I don't know if this apply for all cases, but I just had to extract the Json object that contains only the data. This can be done using any common Json parser, as like as Gson.
my Json Strin is
{"result":[[{"Name":"Mos","Family":"Hos"},{"Name":"Mos","Family":"Hos"}]]}
i connect to rest server and get this string in java but i cant pars this string to a class in java with gson .
what class i need in Java for read this string.
JSon is a format that doesn't depend on the language. You can create it in Delphi/C/even code by yourself and read in any other language.
In Java its easy because there are a lot of thirdparties that handle this.
To name a few:
Jackson
JSON
JavaJSon
FlexJson
I'm sure there are others. I think the most widespread are gson and jackson
Hope this helps
you can use jackson api for this. may this link will help to achieve this.
I amusing Flex for front end building. I have one doubt regarding sending two array of strings from java to flex.
How can I do that?
For example I have a value object defined like below.
class Test
{
String value1[]={"1","2","3"};
String value2[]={"narendra","mani","suresh","kane"};
//Getter and setters goes here
}
Can any one help me on this?
Thanks,
Narendra
This is really a matter of how do you want Flex to communicate with your Java layer, I think the closest to a turn key solution to this is using the BlazeDS jar on the server to do the ActionScript Message Format conversion between Java DTOs and Actionscript DTOs, basically how it works is you code up the Java side then you can have it generate the Java equivalent DTOs that will be tagged with Metadata so when the Java objects are serialized and sent across the wire the client has them as typed objects.
http://opensource.adobe.com/wiki/display/blazeds/BlazeDS
Alternatively you could expose the data as XML using a JSP to generate the XML then just using an HTTPService call to the JSP to get the XML data then since AS3 makes use of E4X parsing the XML into AS3 objects is very easy, lots of examples of doing this just search for e4x AS3 for examples.
Hope this helps,
Shaun
Hello Im trying to pass an array of objects from javascript to java , but how can this be done..??
I've found in some posts that they do this using a hidden input. Is this the only way?
I'm a bit confused. Please tell me what do I need to do to pass my array to the server? and which javascript files and jars do I need to add?
Thanks in advance.
You seem to want a completely baked-in solution. Not sure I can provide that, but here's what I'd do.
Indeed use a hidden input field in a form, where the value of the field is a valid JSON string. Send the form to your server, and in your servlet use a JSON Java library to parse the JSON string.
Here json-lib, gson or Jackson would do. In your case, I'd say json-lib would seem the easiest to use.
To generate the JSON string on the client-side, either use a framework or custom solution. For instance, jQuery has a serialize() function to serialize a form's fields to a JSON object directly, which you can then convert to string. Other frameworks provide similar functions.
To learn more about JSON, be sure to read the JSON Wikipedia entry and to visit the official JSON page (which also gives you a Java implementation of the JSON data-interchange format, though maybe not the most efficient one for processing a lot of data). To make sure your generated JSON is valid, you can use JSONLint.
If the objects are simple enough, you can encode your array as a JSON string. Java has libraries to encode and decode JSON.