Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
We have good support for JSON in java http://blog.locut.us/main/2009/10/14/which-is-the-best-java-json-library.html but what about BSON.
What library do you know that provides BSON support for java?
It should obviously be efficient in runtime.
You can use the MongoDB driver for Java to store a BSON object, then convert that to a String which you can then wrap with JSONObject.
For example, here's how I'll create a regular document:
BasicDBObject obj = new BasicDBObject();
obj.put("name", "Matt");
obj.put("date", new Date());
Then, to get a String representation of the object, simply call:
String bsonString = obj.toString();
Wrap it with a JSONObject and get the date attribute, which should return it in a BSON-compliant format.
JSONObject newObject = new JSONObject(bsonString);
System.out.println(newObject.get("date"));
The resulting output is something like:
{"$date":"2012-08-10T05:22:53.872Z"}
The BSON site is pointing at this
If you want to use it from MongoDB, take a look at this example
In order to get our Model in MongoDB we used google gson to convert our model into JSON first and then we used the JSON util parse method from MongoDB to parse our generated JSON string to a DBObject which you can put in your MongoDB. I don't know about performance to be honest.
There is also a rather new BSON4Jackson project, which allows one to use Jackson for handling BSON data. This means full data binding (to/from POJOs), tree model, even streaming (incremental) reading/writing to degree it can be done with BSON format.
There is also ebson. I've not tried it...
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
I am looking to parse ADA code and construct its AST using Java. My belief was that there's a parser written in Java to parse almost every programming language, however after days of research I haven't found anything.
The only promising tool I've found is libadalang (https://github.com/AdaCore/libadalang) by Adacore. However, this only provides api for Ada and Python (although in the readme file Java is mentioned).
Do you have anything to suggest? If there's no direct way of parsing Ada using Java, is any of you familiar with any library that could work as an intermediate? For example, parse Ada using XXX tool and store the AST in a schema (e.g. XML) and then parse the XML using Java?
I'm one of the Libadalang developers, we have indeed planned adding Java bindings at some point, but this is not a high priority item for the moment.
Having a serializer for a common format is something that would be quite easy to add though, especially to the python API were you have full introspection on the tree.
Here is a JSON serializer for the python API of Libadalang:
import json
def node_to_data(self):
if isinstance(self, ASTList):
return [i.to_data() for i in self if i is not None]
else:
return {n: v.to_data()
for n, v in self.iter_fields(with_properties=False)
if v is not None}
def token_to_data(self):
return {"kind": "Token", "token_kind": self.kind, "text": self.text}
ASTNode.to_data = node_to_data
Token.to_data = token_to_data
ASTNode.to_json = lambda self: json.dumps(self.to_data())
I'll add this to the development version of Libadalang soon!
I think I've read that it is possible to call native C libraries from Java.
You could make a C binding to ASIS (Ada Semantic Interface Specification), and then call that from Java.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a String and I want convert it to a String array without using a JsonObject.
I searched many topics, but I can't solve my problem.
My String is like:
String str="[{"India":{"State":"Gujarat","City":"Rajkot","Pin":360330,"Place":"AT Rajkot"}}]";
And I want output As Array like:
India
Gujarat
Rajkot
360330
AT Rajkot
How can I get an array like that output, without using jsonObject?
You are asking how to parse JSON. You need to use a JsonObject or another JSON library. If you want to learn how to do this, read the several thousand lines of source code in JsonObject and learn how to write a JSON parser. Then, write a JSON parser, which will do what you want.
In other words, what you are asking is not technically incorrect, but ridiculous.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a string that looks like this
[
{"last_name":"Aiken","first_name":"George","state":"VT","party":"R"},
{"last_name":"Allott","first_name":"Gordon","state":"CO","party":"R"},
{"last_name":"Anderson","first_name":"Clinton","state":"NM","party":"L"},
{"last_name":"Bartlett","first_name":"Edward","state":"AK","party":"D"}
]
That is stored in a variable.
How would I add this string to an array so that I can then access the data (no punctuation)
is needed.
The string you have mentioned is a JSON string.
JSON (JavaScript Object Notation) is a lightweight data-interchange
format. It is easy for humans to read and write. It is easy for
machines to parse and generate. It is based on a subset of the
JavaScript Programming Language, Standard ECMA-262 3rd Edition -
December 1999. JSON is a text format that is completely language
independent but uses conventions that are familiar to programmers of
the C-family of languages, including C, C++, C#, Java, JavaScript,
Perl, Python, and many others. These properties make JSON an ideal
data-interchange language.
You should use a parser to parse the JSON string and fetch its elements. There are multiple JSON parser available on internet such as:
Jackson
JSON Simple
GSON
JSON is built on two structures:
A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed
list, or associative array.
An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.
---> source & more info: http://www.json.org/
You just need to parse json object and iterate through json array to store the values in array.
These links may be helpful to you:
Link 1 to parse json
Link 2 to parse json
Link 3 to parse json
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
i have this string of values and don't understand how to get the object array name and values of object items in java.
{
"employees": [
{ "firstName":"Rajesh" , "lastName":"Putta" },
{ "firstName":"Rajesh" , "lastName":"P" },
{ "firstName":"first name" , "lastName":"last name" }
]
}
This is basically a json stirng. Check more about it here:
JSON (JavaScript Object Notation) is a lightweight data-interchange
format. It is easy for humans to read and write. It is easy for
machines to parse and generate. It is based on a subset of the
JavaScript Programming Language, Standard ECMA-262 3rd Edition -
December 1999. JSON is a text format that is completely language
independent but uses conventions that are familiar to programmers of
the C-family of languages, including C, C++, C#, Java, JavaScript,
Perl, Python, and many others. These properties make JSON an ideal
data-interchange language.
You can parse it using a json parser such as
simpleJson
Jackson
GSon
Try these parsers, if you get stuck with the code then share your code and the problem.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Hey what do I have to do to implement the Yahoo Query Language in Java?
Or is this generally possible?
I want to parse the JSON after that.
YQL is interpreted server-side, so there's not much to do in Java. I'd just make a URL, open it, and read the data stream. Just copy the PHP example code, mostly:
String baseUrl = "http://query.yahooapis.com/v1/public/yql?q=";
String query = "select * from upcoming.events where location='San Francisco' and search_text='dance'";
String fullUrlStr = baseUrl + URLEncoder.encode(query, "UTF-8") + "&format=json";
URL fullUrl = new URL(fullUrlStr);
InputStream is = fullUrl.openStream();
JSONTokener tok = new JSONTokener(is);
JSONObject result = new JSONObject(tok);
is.close();
Depending on what you need, you might want to write some code around the URL construction to make it less messy-looking, and you might like a fancier JSON parser like Gson instead of org.json as I've used here.
You might also get some milage out of a more robust HTTP client library that would allow multiple queries on one connection, etc.