How to convert String to an Array In Java? [closed] - 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.

Related

How to I decode this weird string using Python or Java [closed]

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 1 year ago.
Improve this question
I have an encoded string stored in my DB that kinda looks like this (altered for confidentiality):
\x7b22747c
How do I decode this string? (Using either Java or Python)
There is code elsewhere that used tobytes() function to decode it. But I don't own the code so I am not sure what it's doing.
The comments helped in looking in the right place.
Here is what worked for me:
bytes.fromhex('7b22747').decode()
So I just had to remove the \x and then just decode the remaining string which was an encoded hex string.

Regex pattern to find the occurrence of a pattern [closed]

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 2 years ago.
Improve this question
Edit 1:
Please don't go by the example in a literal way. The key values can be of any string. Need not include "key" and "col" in them.
For example, the string can be the following
Ravi,India,Married;John,Canada,Single;Robert,Spain,Unknown
So, if I pass Ravi as the key, I should get India and Married as the result.
I am trying to use the Java regex pattern to find the details from a given string. The structure of the string is as follows:
Key1,Key1Col1,Key1Col2;Key2,Key2Col1,Key3Col2;Key3,Key3Col1,Key3Col2
where ; is the delimiter for each record.
My requirement is to accept the Key (which is Key1, Key2 etc.) and return the corresponding values like Key1Col1,Key1Col2 etc.
This would do it:
(?<=^Ravi,|;Ravi,)[^;]+
https://regex101.com/r/Tcmzhd/1
The link above shows PHP but it should work in Java no problem.

How to get only the "make" and "model" data from this json output in either java or jquery. Java isp preferred [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
How to extract make and model from this json output.
[
"{\"_id\":{\"$oid\":\"5a81d2136da3cd4c41b21509\"},\"make\":\"maruti\",\"model\":\"astar\",\"year\":1998}"
]
Use JSON.parse to convert string to the JS object and then access the properties using dot notation.
You don't even need jQuery for this. Following is an example:
let data = [
"{\"_id\":{\"$oid\":\"5a81d2136da3cd4c41b21509\"},\"make\":\"maruti\",\"model\":\"astar\",\"year\":1998}"
];
data[0] = JSON.parse(data[0]);
console.log(data[0].make, data[0].model);
To extract JSON data in jQuery you can do following.
var d = $.parseJSON(data);
var make = data.make;
var model = data.model;
in make and model you will get your desired output.
Hope it helps.

distinguishing cases to use interger.parseInt and string casting methods [closed]

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 8 years ago.
Improve this question
I am developing a Java program and I'm meeting cases where I get undecided whether to use the casting a string to integer method, or to use the integer.parseInt method. Is there any clear benefit for either of the two methods?
With 'casting to string method', I mean:
String.valueOf(integer);
As far as I know, it's not possible to cast from a String to an int, so using Integer.parseInt seems like the best option here.
Looking at your edits about using valueOf, perhaps this link may help: Integer.valueOf() vs. Integer.parseInt()

JAVA - Parse an "Stringed" Array into an actual Array [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Here's my problem :
I have this String for instance
[[0,0,1],[1,0,0],[1,1,1]]
And I would like to convert it into an actual
int[][]
What's the best way to proceed ?
Thank you in advance !
The string you have is a lot like JSON format. You can use a JSON parser like gson
https://code.google.com/p/google-gson/
It will be able to parse string variants of lots of things.
The default fromJson might work since you are using pretty simple value types, but you can also try to explicitly set the array type.
Type listType = new TypeToken<Integer[][]>() {}.getType();
Integer[][] yourArray = new Gson().fromJson(text, listType);
You can ignore the first [ and the last ] as they are not making a difference.
The you can split the string by the commas and get a String[]. You just need to iterate through that and fill up your int[][].
Use nested loops and the Integer.parseInt method.

Categories

Resources