How can I search a word on JSON format? - java

My JSON contains:
{"state":1,"employee":{"user":"raparra","pass":"1234","work":"taxy_driver"}}
I want to extract the pass (1234). Is there any function to make it easy and fast? Thanks!

Use JSON parser with JsonPath support - it will find and extract only required data that are defined by a query.

Related

Is there any way to deserialize a nonstandard JSON in Java

{
"id":1,
"city":"cityname",
"address":"{"name":"addressName"}"
}
for example
The value of the address field is missing an escape,Is there a way to deserialize it as a string
Yes and No.
Yes:
If there is a reliable pattern for detecting the error, you could potentially write some Java code to insert the missing escapes. Then you parse the corrected JSON using a regular JSON parser.
It might even be possible to write a custom JSON parser that treats a "{" sequence as "{\", and so on. Or modify an existing parser to do that.
No: A regular JSON parser will reject this. AFAIK, no mainstream JSON parser supports arbitrary non-standard (i.e. broken!) JSON variants.
A better idea is to fix whatever is generating the broken JSON. Or charge the customer who wants you to support this garbage a LOT OF MONEY because their data doesn't conform to the agreed requirements. (Assuming that the agreed requirements said JSON.)

Cleanest way to deserialize a non-standard (wrong) format of list of JSON string that doesn't have quote

I'm trying to deserialize a Java String to a List of String. Due to some reason, the input may come in two formats:
"[\"string1\", \"string2\"]"
or
"[string1, string2]"
The library I'm using is Jackson databind.
For the first case, it's a typical, easy case that Jackson supports.
For the second, I understand it's not a correct format of JSON and I can hack to achieve the goal by splitting this String by , and remove []s etc, but just would like to know if someone knows a clean way to deserialize something like that.
Thanks in advance.
To answer your question, you can look into YAML parsers. :)
Jackson has an extention for YAML support so that would be your clean solution.
YAML is a superset of JSON so it can parse any valid JSON... as well as many more complex transcripts (like strings without ").

How to remove particular String from text file using java

I have a text file which consists of json data and I do not need some of the data fields. How do I remove these strings and bring them to another form?
Current Data:
[{"EMPTYPE":"21","EMPSUM":"1","VERSION":2.2,"UID":212121,"EMPID":454354,"EMPNAME":"abc","EMPGPRID":123,"AID":121,"DATE":"2015-07-07","EMPGPRID":21}]
Required Data:
[{"EMPTYPE":"21","EMPID":454354,"EMPNAME":"abc","EMPGPRID":123}]
Parser's or delimeters
"(?!EMP(?:TYPE|ID|NAME|GPRID)")[^"]*":.*?(?:,|(?=}))
Try this.Replace by empty string.See demo.
https://regex101.com/r/hR7tH4/18
You should look into Jackson for the json parsing (it will take the json string and make more like a regular Java object). It also has a nice remove method you can use (if you know what you don't want) or you can simply make a new object with just the info you like. It's then easy to serialize it back to json.

How to decode XML file without using XML Decoder?

I have a XML file which was encoded using java.beans.XMLEncoder. I cannot use java.beans.XMLDecoder to decode it, as the class of encoded object is not present in my project. Is there a way to obtain values in xml without using java.beans.XMLDecoder, xmlDecoder.readObject() method ?
There are several APIs for parsing XML in Java which don't require you to deserialize an object. Once you have a DOM, XPath is a useful way to query its contents. You will need to know what you're looking for, though.

How to add a URL String in a JSON object

I need to add a URL typically in the format http:\somewebsite.com\somepage.asp.
When I create a string with the above URL and add it to JSON object json
using
json.put("url",urlstring);
it's appending an extra "\" and when I check the output it's like http:\\\\somewebsite.com\\somepage.asp
When I give the URL as http://somewebsite.com/somepage.asp
the json output is http:\/\/somewebsite.com\/somepage.asp
Can you help me to retrieve the URL as it is, please?
Thanks
Your JSON library automatically escapes characters like slashes. On the receiving end, you'll have to remove those backslashes by using a function like replace().
Here's an example:
string receivedUrlString = "http:\/\/somewebsite.com\/somepage.asp";<br />
string cleanedUrlString = receivedUrlString.replace('\', '');
cleanedUrlString should be "http://somewebsite.com/somepage.asp".
Hope this helps.
Reference: http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#replace(char,%20char)
Tichodroma's answer has nailed it. You can solve the "problem" by storing valid URLs.
In addition, the JSON format requires that backslashes in strings are escaped with a second backslash. If the 2nd backslash is left out, the result is invalid JSON. Refer to the JSON syntax diagrams at http://www.json.org
The fact that the double backslashes are giving you problems actually means that the software that is reading the files is broken. A properly written JSON parser will automatically de-escape the strings. The site I linked to above lists many JSON parser libraries written in many languages. You should use one of these rather than trying to write the JSON parsing code yourself.

Categories