Error while getting JSON when data has spaces - json

I am facing issue while fetching json data when data has white spaces .
I am using spring,
In spring controller i am doing like below,
String json=new Gson().toJson(agents, new TypeToken>() {
}.getType())
I am able to see proper json in logger too, after that I am setting to model attribute like below one
model.addAttribute("json",json);
here model is 'org.springframework.ui.ModelMap'
In JSP I am able fetching data till no space found.
Example :
Original JSON String: ILOVE JAVA
Fetching string in JSP: ILOVE
Only please help me out this with out using encoding string.

Related

How to display Blob /Json data field in Liferay 7.2?

I am using a service builder that is retrieving the form data fine from mysql db. I have a field that has the json data and I tried to map it using object mapper and using com.fasterxml.jackson.databind.ObjectMapper to display the json content. However, the Blob Data is shown as: com.mysql.cj.jdbc.Blob#4ca74f7f
How do I actually get/extract the data from the storing link above? Here is my code snippet:
for (ddmcontent MyTemp : myList) {
System.out.println("Content ID : "+myList.getContentId());
System.out.println("User Blob Data : "+myList.getData());
Blob responseBody =myList.getData();
ObjectMapper objectMapper = new ObjectMapper();
List<ModelData> myDat = objectMapper.readValue((DataInput)
responseBody,objectMapper.getTypeFactory().constructCollectionType
(List.class,ModelData.class));
for (ModelData dt : myDat) {
System.out.println("User Name : "+dt.Name);
System.out.println("Users Email : "+dt.Email);
}
}
Please note, I have defined my ModelData elements as all String.
Any suggestion? What am I missing?
Thanks in advance!
The toString() representation hints at the object's type com.mysql.cj.jdbc.Blob. If you look up its javadoc (or the interface it implements) you'll see the options that you have to decode the contents of the Blob, namely getting an InputStream or a byte[] representation, which you'd have to subject to the correct character set decoding to turn it into a String.
Make sure you nail the character set by testing it with all kinds of Unicode content, so that you don't have to fix badly encoded database content later when your table contains a lot of data in unknown encodings.
As you're using Liferay's Service Builder, you might want to share the relevant parts of your service.xml (optional model-hints.xml) to check for an easier implementation.
I finally got this working by changing the field in question to String and addining a max length to certain number of Char
Thanks!

how to parse a json text on angular array of object .? and display it on ng 2 smart table

I'm doing the maintenance of an angular website, I have a problem of display in the ng2 smart table , in a column it displays the json of an object, this is the error that the developer has made by receiving the list of transaction where the user and items are a json text,
how can I parse them
ps this is the code :
this.data.doData('transaction','get').subscribe({
next:res=>{
console.log(res);
this.dataSource = [...res['transactions']]
console.log(this.dataSource)
},error:err=>{
console.log(err);
}
})
So actually you are getting the JSON string. To use it just do JSON.parse(items) and JSON.parse(user) and you will get the parsed JSON (array for the items and pbject for the user)
You can also use map in your service file and handle this parsing their so when you subscribe to the data in your component's ts file you will get the parsed form.
UPDATE:
Look at the quotes (quotes matching you would know from basic rules of strings, i.e. either single or double should be enclosing the main string). So when you were JSON parsing the string it wasn't taking it correct format to parse it.

Iterating through JSON format

i have the following problem: i am sending a JSON File from a webservice into a extension to Qlik Sense and i am getting the following response:
"{"rownumber":1,"ID":1}{"rownumber":2,"ID":2}"
The type of the response is string. My question is how can i change the format to iterate through each element of this.
I have already tried the JSON.parse(result) and JSON.stringify(result) functions but the result was nothing that i can work with. Last one changed the
result to:
"{"rownumber":1,"ID":1}{"rownumber":2,"ID":2}"

Scala, Json & Spring Boot Rest

I am building a Rest API using spring boot and Scala based on the below suggestion. I am successfully able to call the methods in Scala but the case class is not working the way it is designed.
When I try to create a Json using net.liftweb.json, I am getting additional string as "$outer":{}. I don't want this additional string in my output.
Note that the spring boot is expecting class instead of object. I suspect this could be an issue. Can you help me on this.
My case class looks like this:
case class BasicSrch(size: String, query: Match)
Erroneous output
{"$outer":{},"size":"10","query":{"$outer":{},"match":{"$outer":{},"_all":{"$outer":{},"query":"SNOW","operator":"and"}}}}
Expected Output
{"size":"10","query":{"match":{"_all":{"query":"VALLE","operator":"and"}}}}
If you are writing UTF-8 encoded JSON to the byte array or OutputStream then try jsoniter-scala: https://github.com/plokhotnyuk/jsoniter-scala
It will not serialize any other fields except those which are defined in a primary constructor of the case class.

String not valid UTF-8 when inserting json file into mongodb

i am storing a json file produced by cucumber json report into mongodb. But i get "String not valid UTF-8” when inserting json into mongodb.
Because the feature file has 年/月/日 , this is represented as "\u5e74/\u6708/\u65e5" in the json file.
This is what i am doing to store it in mongo
json_string=File.read(file_path)
data = JSON.parse(json_string)
#col.insert(data)
I can see that after JSON.parse, the already encoded string further changes to "\x90\u0013s/\x90\u0013s/\x90\u0013s"
exception is happening at insert statement.
any help appreciated.
I tried the following but still does not work
json_string=File.read(file_path,:encoding => 'UTF-8')
data = JSON.parse(json_string.force_encoding("UTF-8"))
Using Ruby 1.9.3