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
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.
This question already has answers here:
Deserializing a java serialized file in C#
(3 answers)
Closed 6 years ago.
I have a very old Java application that I am rewriting in .net, I can't change the Java code or the old files in any way.
They have created and stored 10,000+ files that match the format described in this article, a bunch of serialized Java objects.
Question
How can I parse these Java objects in c#?
Is this even possible?
In the end, if I can read in and serialize the data, I can store it in a more universal format. When I try to deserialize the file I reach an exception, usually telling me the binary format is not valid.
A similar question has been asked here.
You have a few options. One is to write a C# class capable of reading objects in Java's serialized format (the one you linked) but this is likely very time consuming. Using C#'s native deserialization algorithm won't work because the format is different (as you've encountered).
An easier alternative is to read the objects from the files using a Java program, and save them as a more universal format such as JSON. (As recommended in an answer to another question here)
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
here is the non-oop way of doing it:
When someone presses a day on a calendar, we need to find out the day, and path. Here is the non-oop way of storing that information:
A string was created with a "-" delimiter between each piece of info we need, like this:
12-c:\files\john_doe.png
Then it was stored in an array. However, to retrieve the data, we then use the "split" function like this.
for (int t = 0;t < day_and_path.length;t++)
{
String[] day_from_db = day_and_path[t].split("-");
String day_db = day_from_db[0];
String path_db = day_from_db[1];
However the OOP way is, make a class with properties: day, path. Then store them into an array of objects.
Which way is better and why?
If you use String#split, you'll be dealing with an array of strings. If all you do is assign the strings to local variables, then there is no point in making a class for that.
If you want to hold on to the string parts and pass them around to other methods, then it begins to make sense to have an object which will conveniently encapsulate those strings.
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.
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...