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

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)

Related

How to deserialize json to class with missing fields

I'm following a tutorial where one of the tasks is to deserialize json using gson. I looked at several SO posts, but couldn't find one that addressed my issue. I get a string representation of an array in JSON whose fields look like below:
photoJsonArray
[{"id": "28857102437",
"owner":"9457266#N02",
"secret":"a5f02e005f",
"server":"857",
"farm":1,
"title":"",
"ispublic":1,
"isfriend":0,
"isfamily":0,
"url_s":"https:\/\/farm1.staticflickr.com\/857\/28857102437_a5f02e005f_m.jpg" ...and so on
I want to deserialize this list into a list of GalleryItem objects, which only includes the id,url_s and id fields from the JSON array.
I tried the following approach to converting the JSON string to a list of Gallery Item objects, but all the fields ended up with a null value.
Type photoType = new TypeToken<List<GalleryItem>>(){}.getType();
List<GalleryItem>galleryList = g.fromJson(photoJsonArray.toString(),photoType);
I would really appreciate if someone could point me in the right direction!
after some research I discovered that all the fields from the JSON string need to be declared in the class in order to deserialize the JSON into that class. Seems a bit overkill if all you need to use is a couple fields, but hope this helps.

JSON won't parse Java list object

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\"}"

How to represent nested json objects in a cucumber feature file

I have a need to represent JSON object in the feature file. I could use a json file for this for the code to pick up. But this would mean that i cant pass values from the feature file.
Scenario: Test
Given a condition is met
Then the following json response is sent
| json |
| {"dddd":"dddd","ggggg":"ggggg"}|
Above works for a normal json. However if there are nested objects etc then writing the json in a single line like above would make the feature very difficult to read and difficult to fix.
Please let me know.
You can use a string to do that, it makes the json much more legible.
Then the following json response is sent
"""
{
'dddd': 'dddd',
'ggggg': 'ggggg',
'somethingelse': {
'thing': 'thingvalue',
'thing2': 'thing2value'
}
}
"""
In the code, you can use it directly:
Then(/^the following json response is sent$/) do |message|
expect(rest_stub.body).to eq(message)
end
or something like that.

Deserialize a string to json

I want to know whether deserialize converts json to string or string to json.I need my string to be returned as Json so i used deserialize, but unsure about its syntax.Can anyone direct me correctly.
My code
JavaScriptSerializer datajson = new JavaScriptSerializer();
var objec = datajson.Deserialize<string>(data);
return Json(objec,JsonRequestBehavior.AllowGet);
Serialisation is the act of taking objects and turning them into something more persistable or communicable, i.e. turning objects into JSON, XML or binary data.
Deserialisation is the act of taking serialised data and turning it back into objects.
So in your case, if you want to turn your objects/variables into JSON, the process is called serialisation.
Your code, assuming it is MVC C# (you may wish to add these tags to your original post), appears to be deserialising a JSON encoded string into a string, then serialising it back to JSON again when it returns the view. I'm not sure why you would want to do this. You should be able to simply do:
return Json(data, JsonRequestBehavior.AllowGet);

Understanding VBJSON class set up in Excel 2010

I have downloaded and set the JSON parser for VB6 from this website:
VB-JSON
I cannot understand how this module works. I successfuly set it in Excel 2010 and I think I also understand the JSON format but I don't know how this class extracts the items. I tried to parsed the following text in the JSON format
{"realms":[{"type":"pvp","population":"low","queue":false,"wintergrasp":{"area":1,"controlling-faction":1,"status":0,"next":1356724174636},"tol-barad":{"area":21,"controlling-faction":1,"status":0,"next":1356723246779},"status":true,"name":"Kor'gall","slug":"korgall","battlegroup":"Cruelty / Crueldad","locale":"en_GB","timezone":"Europe/Paris"},{"type":"pve","population":"medium","queue":false,"wintergrasp":{"area":1,"controlling-faction":0,"status":0,"next":1356724425638},"tol-barad":{"area":21,"controlling-faction":0,"status":0,"next":1356723369780},"status":true,"name":"Alonsus","slug":"alonsus","battlegroup":"Cruelty / Crueldad","locale":"en_GB","timezone":"Europe/Paris"}]}
A browser will return this content when the following address is used:
Alonsus, Kor'gall
I wrote a code similar to this
Dim objJSON As Object
...
strData = objJSON.Item("Realms")(1).Item("Type")
but it causes errors: "Object variable or With block variable not set". I expected to get "pve" value. I am confused because it starts with an object name "realms" followed by an Array. The array is supposed to be returned as a collection. I would be grateful for any help with that.
Thanks
set objJSON = JSON.Parse(jsonFromUrl)
strData = objJSON.Item("realms").Item(1).Item("type")
Keys inside json is case-sensitive.