(GSON) Collection to Json String [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 the following row in my function:
Collection<Song> songs = a.getListOfSongs();
There are all required annotations in the Song class.
Need to return JSON String using GSON lib.
Thank you!

Use new Gson().toJson(songs). Although when you try to make Song bean from this json, you might have to face some casting issues like, int value would have converted to double, char to string...as GSON uses its intelligence to resolve such types

Gson gson = new Gson();
String json = gson.toJson(songs);

Related

Make a List<List<BigDecimal>> drom json value using Jackson in Java [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 3 years ago.
Improve this question
i'm getting this json:
{"channel":"123","data":"[[\"7132.09114\",\"0.00800000\"],[\"7131.71462\",\"0.08900000\"],[\"7122.94432\",\"0.75000000\"],[\"7122.87085\",\"0.20000000\"],[\"7122.38788\",\"0.00300000\"]]","event":"updated"}
How can i make List< List< BigDecimal>> from "data" value ? I was trying to get it using TypeReference in readValue(), but I got NPE, because this array inside of "".
Class which holds the data:
public class Result {
#JsonProperty("channel")
private String channel;
#JsonProperty("data")
private List<List<BigDecimal>> data;
}
Why not use something like this?
String jsonInput = "[[\"7132.09114\",\"0.00800000\"],[\"7131.71462\",\"0.08900000\"],[\"7122.94432\",\"0.75000000\"],[\"7122.87085\",\"0.20000000\"],[\"7122.38788\",\"0.00300000\"]]";
List<List<BigDecimal>> myObjects = objectMapper.readValue(jsonInput, new TypeReference<List<List<BigDecimal>>>(){});

Masking some value inside Json string in Java [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 6 years ago.
Improve this question
Suppose i have below Json String:
{
"name":"noor",
"pass":"12345"
}
I want to mask pass value using Regex, like below
{
"name":"noor",
"pass":"*****"
}
How i can do it, using Java regex?
Try this:
"pass":"(.*?)"
As seen on: https://regex101.com/r/cK4bD0/1
try this
String jsonString = "{ \"name\":\"noor\", \"pass\":\"12345\" }";
String result = jsonString.replaceAll("(?<=pass\":\")(.*?)(?=\")", "*****");
System.out.println(result);
{[^}]*"pass"\s*:\s*"(.*?)"[^}]*}
Here is the DEMO: https://regex101.com/r/nL3tP2/2

Jquery function to convert java data to json [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
Is there a jquery function that will convert a java data object to a json?
I am using the HTTPResponse object to serve up my object to my ajax, which returns a String object.
note:
JSON.parse wont work
I cant use any json parsing libraries
Try using $.getJSON
var onSuccess = function(data) {
//data is now a javascript object
};
$.getJSON("your-url").done(onSuccess);

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.

How to convert String to an Array In 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 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.

Categories

Resources