I am facing one issue but it occurs randomly any time not always. I am not sure what is the issue.
What I am trying is I have an JSON string and I am converting it to DBObject and then push/insert it to mongo DB. below is the code for same.
Here final_json_str is JSON string which I am reading it from input file and then converting it to DBOject and then inserting it to Mongo DB.
val dbObject: DBObject = JSON.parse(final_json_str).asInstanceOf[DBObject]
But some time I am getting error as CastClassException cannot convert java.lang.String to com.mongodb.DBObject.
Can anyone one help me, Am i doing some thing wrong in this. Please let me know if more details are required.
Thanks a lot in advance
from the signature of JSON.parse
public static Object parse(final String jsonString)
One can see it can actually return any Object. Looking a bit further in the actual code, the parse will choose what method to return with a big switch on the first character of your String.
In the case it matches either ' or " the returned Object will be a String which you try to cast into a DBObject.
Related
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)
When creating a SuperObject from a string, it might happen that the string is no valid JSon.
Unfortunately the command SO doesn't raise an exception in that case.
I end up with a object where I THINK I can store data in, but the "stored" data goes nowhere and is lost.
example:
MySo:=SO('{}');
MySO.S['ok']:='test';
Memo1.Lines.Add(MySO.AsJSon(True, False));
MySo:=SO('');
MySO.S['fail']:='mimimi';
Memo1.Lines.Add(MySO.AsJSon(True, False)); // returns '""' ??!??
How can I check if the string was converted successfully into a valid and working SuperObject?
whorsyourdaddy's comment pointed into the right direction.
To be able to store INTO a JSon you need a stObject. You can check for that this way:
if not MySo.IsType(stObject) then raise....
I want to save with JOSE4J the JSON representation of a RsaJsonWebKey object in JSON format and then recreate a RsaJsonWebKey object from it again. I have the marshalling part:
RsaJsonWebKey rsaJsonWebKey = RsaJwkGenerator.generateJwk(2048);
String jwkjson = rsaJsonWebKey.toJson(JsonWebKey.OutputControlLevel.INCLUDE_PRIVATE);
but how to unmarshall it and recreate RsaJsonWebKey. That is where I'm stuck as I don't see a constructor of RsaJsonWebKey allowing that.
The question is related to this question
JWT becomes invalid after restarting the server
but it doesn't explain how to unmarshall.
The bottom half of the example in JWT becomes invalid after restarting the server does show how to get to a JsonWebKey/PublicJsonWebKey object from a JSON string. Using PublicJsonWebKey publicJsonWebKey = PublicJsonWebKey.Factory.newPublicJwk(jwkJson); will do the parsing/unmarshalling and can be cast to RsaJsonWebKey if need be.
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);
I'm using Swift to parse JSON strings. This is the code:
var jsonStr = "..." // JSON string
var data = jsonStr.dataUsingEncoding(NSUTF8StringEncoding)
var error: NSError?
var array = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &error) as [AnyObject]
And this code works almost all the time. But today I tried to parse a new string input but it crashed my app in the last line. I couldn't even see the error message in the "error" variable... It crashed before that variable as updated with the error info.
The JSON string that I'm trying to parse is here: http://pastebin.com/wf6jtNhf
I'm confident that my JSON string is valid for two reasons:
I validated the input using jsonlint.com
I created a Objective-C version of the code above and it successfully parsed the same input
Can anybody see a reason why I can't parse this string or should I assume that the NSJSONSerialization class is bugged in Swift? I'm using Xcode Beta 3.
Edit 1:
Apparently there is a lot of UTF-16 characters in my string (emoji characters). Is there a key to parse the string keeping those characters? I tried NSUTF16StringEncoding the code below, but it didn't work:
var data = jsonStr.dataUsingEncoding(NSUTF16StringEncoding)
Edit 2:
I posted this same question in the Apple Developer forum and apparently there is indeed a bug in the Swift version of NSJSONSerialization.JSONObjectWithData() when there are emoji characters in the data. I hope this gets fixed in the final version.
Also, changing my variable from [AnyObject] to as? Dictionary, as some suggested below, didn't crash my app anymore.
Testing this on my computer, you're crashing because you're casting as [AnyObject], and you're getting a nil result back out. If you change that to as? [AnyObject] then it will allow the return value to be nullable and you'll then be able to print the error.
As for the reason it's failing to parse, I'm not quite sure yet. The culprit seems to be the following chunk, not sure why it's invalid, perhaps some of the UTF is giving the parsing code problems.
"id": "#babiminkah",
"nome": "B\u00E1rbara Santos",
"imagem": "http://pbs.twimg.com/profile_images/490247572175327233/w4dXqfPm_bigger.jpeg",
"texto": "amanh\u00E3 tem b\u00F3 do catarina..... na friends \uD83D\uDE02\uDE02\uD83D\uDE02\uDE02\uD83D\uDE02\uDE02\uD83D\uDE02\uDE02\uD83D\uDE02\uDE02\uD83D\uDE02\uDE02\uD83D\uDE02\uDE02\uD83D\uDE02\uDE02\uD83D\uDE02\uDE02\uD83D\uDE02\uDE02\uD83D\uDE02\uDE02\uD83D\uDE02\uDE02",
"horario": "2014-07-18 17:43:04"
}
Edit1: Ripping out more pieces of that chunk, it is the texto field's value that's causing the problem. If you remove all of the value after and including \uD83D it will work. These unicode values seem to be the problem.
Edit2: According to this, http://www.fileformat.info/info/unicode/char/d83d/index.htm , D83D is not a valid unicode character. It seems likely some of the subsequent ones are not either.