JSON won't parse Java list object - json

I am working with JSON and i have data to send in JSON form.I have field "stavka" which should be list of JSON objects,[{},{}...],and i have niz=["{"redni_broj":"3","naziv_robe_usluge":"gwe","kolicina:2","umanjeno_za_rabat":"43","ukupan_porez2":"33"}", "{"redni_broj":"9","naziv_robe_usluge":"12a","kolicina:55","umanjeno_za_rabat":"12","ukupan_porez2":"2"}"],and so I did "stavka":niz within JSON.stringify function,because niz looked as list of JSON objects..But when I send it,it says
"Can not construct instance of..no String-argument constructor/factory
method to deserialize from String value
('{"redni_broj":"3","naziv_robe_usluge":"gwe","kolicina":"123","jedinica_mere":"12","jedinicna_cena":"54","vrednost":"12","procenat_rabata":"1","iznos_rabata":"2","umanjeno_za_rabat":"43","ukupan_porez2":"33"}')
Does someone know why this happens?Shouldn't this be working?I thought it is the same as explicitly doing "stavka":[{"redni_broj":"3","naziv_robe_usluge":"gwe","kolicina:2","umanjeno_za_rabat":"43","ukupan_porez2":"33"}",{"redni_broj":"9","naziv_robe_usluge":"12a","kolicina:55","umanjeno_za_rabat":"12","ukupan_porez2":"2"}]
Thanks in advance!

there are some syntax error in your code. First, you can't write the array like this
niz = ["{"redni_broj":"3","naziv_robe_usluge":"gwe","kolicina:2","umanjeno_za_rabat":"43","ukupan_porez2":"33"}", "{"redni_broj":"9","naziv_robe_usluge":"12a","kolicina:55","umanjeno_za_rabat":"12","ukupan_porez2":"2"}"],
maybe you means niz is an array like niz = ['xx', 'xx'], but you just the " not ', "{"redni_broj":"3","naziv_robe_usluge":"gwe","kolicina:2","umanjeno_za_rabat":"43","ukupan_porez2":"33"}" is not a string, it is wrong way to use like that.
'{"redni_broj":"3","naziv_robe_usluge":"gwe","kolicina:2","umanjeno_za_rabat":"43","ukupan_porez2":"33"}' is right way.
Or you can try to use it like this way
"{\"redni_broj\":\"3\",\"naziv_robe_usluge\":\"gwe\",\"kolicina:2\",\"umanjeno_za_rabat\":\"43\",\"ukupan_porez2\":\"33\"}"

Related

Newtonsoft.Json.JsonSerializationException - Cannot deserialize the current JSON object

Lot of similar questions but still not able to make it. Here is my
code in vb.net and this is the json response. enter image description here I know this is because of [] but i'm using list don't know still getting the same error.
This is because you are receiving a JSON array and not a single JSON object.
You need to deserialize the array like this -
Dim Items_Array = Newtonsoft.Json.JsonConvert.DeserializeObject(Of T())(jsonString)

how to parse dynamic json object in Typescript

I have a json object in my typescript which is returned from server side. One of the keys is dynamic. How do I parse and extract the value for that key.
Ex: serverResponse.detailsLMN.allList
Ex :serverResponse.detailsLMN.deleteList
In the above , 'LMN' is dynamic.
It can be serverResponse.detailsLMN.allList or serverResponse.detailsPQR.allList.
Assuming,
const temp = 'LMN' or 'PQR', how can I use temp to parse JSON object here.
Like : serverResponse.details{temp}.allList
Not sure if I understood your question correctly. But try doing
let data = JSON.parse(serverResponse);
((JSON.stringify(serverResponse)).includes("LMN")) ? serverResponse.detailsLMN.allList
: serverResponse.detailsPQR.allList
^The above code is the same as the if statement. If you don't know the ES6 ternary conditional statement then here's a link:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator
To parse, simply use JSON.parse(). To extract the value, since I can't see the format of the output, it's better to console.log(serverResponse) the whole response and then walk through the Object in Chrome console to see how to get to your specific value.

Converting a string of an array into an array in JS

I have a project I'm working on that implements MySQL, React and Express.js. I need to save an array into MySQL, but there are currently no ways to save an array, as far as I could see, so I was forced to convert it into a string. When I get it back from Express to the client, it's obviously a string, so I can't access the data. This array is used for a graph, mainly. What are some of the ways I can convert this string back to an array?
You can use JSON.parse() to convert a string into an array.
const response = "[1,2,3]";
console.log(JSON.parse(response));
You can store your json object (including arrays )in form of text in mysql database.What you have to do is JSON.stringify("your array") and persist it in database.And while you are retrieving it back from database you can JSON.parse() to get it in form of JavaScript object
Depends on how you formed the string. If you used , for joining the elements, then you can use javascript's string.split() method.
let str = '1,2,3,4';
let arr = str.split(',');
Just pass in whatever delimiter you used to join the elements.
OR
If you're saving elements as a json string, then use JSON.parse(str) as shown by Nils Kähler in his answer

How to create JSON string in Sequel Pro TEXT editor?

I figure this may be an unconventional way of creating a JSON string. But, I really need to just be able to make it right in "Sequel Pro" like this
I want to be able to just edit it right there like that. But when I receive the string on the client end, then try to use as3 JSON.parse function on it, it gets an error..."SyntaxError: Error #1132: Invalid JSON parse input."
private function storyTextCallBack(r:Object):void
{
//storyText is an 'Object'
storyText = JSON.parse(r.text);
}
But this is how my client is actually getting it, and it's what I think is breaking the JSON.parse function.....
[\n\t{\n\t\ttext: "hello this is some json test stuff",\n\t\tduration: "5000"\n\t},\n\t{\n\t\ttext: "this is the second line in that json object thing",\n\t\tduration: "3000"\n\t},\n\t{\n\t\ttext: "this is the third and final line in that json object thing",\n\t\tduration: "8000"\n\t}\n]
anyone have any ideas how I can fix this?
The object names in your JSON need to be inside quotes:
"text":"example text"
You can check if you have a valid JSON object with this parser: http://json.parser.online.fr

Sinatra route returning enumerator instead of json object

I am trying to do the following in a sinatra route:
get '/posts/:id' do
Post.find(params[:id]).to_json
end
But this is returning an enumerator.
How do I access a single object in json format?
PS I'm using datamapper
EDIT
I managed to return the json value by using get instead of find:
get '/posts/:id' do
Post.get(params[:id]).to_json
end
If someone can explain why I will accept answer so not to waste the question :)
find doesn't exist in DataMapper. It's a method of Ruby's Enumerable, that returns an enumerator object ; which you're trying to convert to JSON.
DataMapper objects apparently implement Enumerable, that's why you don't get an undefined method exception.
Enumerable#find: http://ruby-doc.org/core-1.9.3/Enumerable.html#method-i-find
Find method return an enumerator not the entry.
If you expect only ONE entry : try this
Post.first(params[:id]).to_json