I'm currently testing the new Domino V10 LotusScript NotesJSONNavigator classes, and trying to parse long strings using this code:
dim session as New NotesSession
Dim jsonReader as NotesJSONNavigator
Dim stream as NotesStream
Set stream = session.CreateStream
stream.WriteText("*** Populate stream with long JSON here ***")
stream.Position = 0
Set jsonReader = session.CreateJSONNavigator(stream.ReadText)
Once the stream contains more than 64k of text, the final line of code produces an error as follows:
"Unable to Parse JSON string:
Missing a closing quotation mark in string, offset 65535."
If there is a 64k limit, this is a real shame, because the performance of NotesJSONNavigator appears to be very good. Is there any workaround for this if so?
Related
I wish to read the entire contents of my appsettings.json file into NewtonSoft.JSON so I can parse it using NewtonSoft.
This is due to being able to use full JSON paths with NewtonSoft (filtering etc.)
I basically want to just read my entire appsettings.json file as a string.
I already have it working in regardings to a Configuration.
Private Shared Function InitConfig() As IConfigurationRoot
Try
Dim builder = New ConfigurationBuilder() _
.AddJsonFile("appsettings.json", True, True) _
.AddEnvironmentVariables()
Return builder.Build
Catch ex As Exception
Console.WriteLine(ex.Message)
Return Nothing
End Try
End Function
However I don't want to choose specifics i.e. config("test1:test:bot_token"). I wish to just read the entire string, however I don't appear to be able to get this from the ConfigurationRoot.
Cheers
Went about this all the wrong way - just used a streamreader to read the appsettings.json file.
Complete brain fart moment.
Dim tr As TextReader = New StreamReader("appsettings.json")
Dim stream = tr.ReadToEnd
I need to know how to populate datatable from JSON string in vb.net code.
the JSON string
{"success":true,"message":"","result":[{"Paper1:null,"StateId":"57ee","School":"1A","Received":"2018-07-03T08:10:05.22","TimeS":"00","STAT":"98","ScoreCard":"76"},{"Paper1:null,"StateId":"52ef","School":"1A","Received":"2018-07-03T08:10:05.22","TimeS":"00","STAT":"88","ScoreCard":"57"}]}
I need to know how to populate above string to a datatable
The above string is in a WebResponse. So is there any way to read the webresponse (here I have used a streamreader, or any other good method) and populate to a datatable?
my code which I got above string.
Dim rqst As WebRequest = WebRequest.Create(uri_variable)
Dim res_p As WebResponse
rqst.Method = "GET"
rqst.Headers.Add("apisign:" & sign)
res_p = rqst.GetResponse()
Dim reader As New StreamReader(res_p.GetResponseStream())
Dim JSON_String as string = reader
P.S: EDIT: I included a screenshot for the user "CruleD"
https://i.stack.imgur.com/EYbP0.png
Heh, this again.
Imports System.Web.Script.Serialization ' for reading of JSON (+add the reference to System.Web.Extensions library)
Dim JSONC = New JavaScriptSerializer().DeserializeObject(JSON_String)
Inspect JSONC with breakpoint. You will see how it looks then you decide what you want to do with it.
Comparing files not working as intended
Similar answer I gave in another thread.
Edit:
JSONC("result")(0)("Quantity")
First you get the contents of "result", then you select which collection you want, in this case first (so 0) then you search for whatever key you want again eg "Quantity" like you did for result originally.
There are other ways to do the same thing, but this one should be pretty straight forward.
There are 3 types of deserialization, check them out if you want.
I'm trying to deserialize a JSON string in vb.net. The problem I'm having is (I think) because the string is an array of JSON objects as opposed to a single JSON object (in all honesty this is a bit of a guess). I think this because the file starts with [ and ends with ] and between each pair of {} there is a comma.
Here's a link to the data I'm trying to work with:
data link
I'll include the first few lines at the bottom of the question for reference (it's a bit long to include in it's entirety). I'm using Newtonsoft to parse the string but it isn't working:
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
...
Dim json As String = getCrimeData(area)
Dim ser As JObject = JObject.Parse(json)
Dim data As List(Of JToken) = ser.Children().ToList
Dim output As String = ""
For Each item As JProperty In data
item.CreateReader()
Select Case item.Name
End Select
Next
No idea why it's not working the error message I get is:
Newtonsoft.Json.JsonReaderException: 'Additional text encountered after finished reading JSON content: ,. Path '', line 1, position 289.'
For reference here is the first few lines:
[{"category":"anti-social-behaviour","location_type":"Force","location":{"latitude":"52.961180","street":{"id":1066985,"name":"On or near Deabill Street"},"longitude":"-1.072366"},"context":"","outcome_status":null,"persistent_id":"","id":62066811,"location_subtype":"","month":"2018-01"},{"category":"anti-social-behaviour","location_type":"Force","location":{"latitude":"52.989060","street":{"id":1061254,"name":"On or near Park\/open Space"},"longitude":"-0.988926"},"context":"","outcome_status":null,"persistent_id":"","id":62065597,"location_subtype":"","month":"2018-01"},{"category":"anti-social-behaviour","location_type":"Force","location":{"latitude":"52.958492","street":{"id":1067012,"name":"On or near Mallard Road"},"longitude":"-1.064310"},"context":"","outcome_status":null,"persistent_id":"","id":62066947,"location_subtype":"","month":"2018-01"},{"category":"anti-social-behaviour","location_type":"Force","location":{"latitude":"52.964901","street":{"id":1066875,"name":"On or near Asper Street"},"longitude":"-1.072257"},"context":"","outcome_status":null,"persistent_id":"","id":62065125,"location_subtype":"","month":"2018-01"},{"category":"anti-social-behaviour","location_type":"Force","location":{"latitude":"53.010153","street":{"id":1070264,"name":"On or near Old Tannery Drive"},"longitude":"-0.997808"},"context":"","outcome_status":null,"persistent_id":"","id":62065476,"location_subtype":"","month":"2018-01"},{"category":"anti-social-behaviour","location_type":"Force","location":{"latitude":"52.987829","street":{"id":1068825,"name":"On or near Supermarket"},"longitude":"-1.036609"},"context":"","outcome_status":null,"persistent_id":"","id":62065402,"location_subtype":"","month":"2018-01"},
dim json2=Newtonsoft.Json.Linq.JObject.Parse("[{""currency_code"":""1ST"",""cash"":""0"",""reserved"":""0""},{""currency_code"":""8BT"",""cash"":""0"",""reserved"":""0""}]")
The doublequotes are just escape characters. The actual string is
[{"currency_code":"1ST","cash":"0","reserved":"0"},{"currency_code":"8BT","cash":"0","reserved":"0"}]
Which as you can see is a legitimate json.
Basically it looks like a legitimate json array. I want to turn that into an array of strings
Basically I want to turn that into
{
"{""currency_code"":""1ST"",""cash"":""0"",""reserved"":""0""}",
"{""currency_code"":""8BT"",""cash"":""0"",""reserved"":""0""}"
}
First I parse that into a jobject andd............ I failed.
It's a legitimate json. Why I can't parse that into a jobject?
Some background.
I created 2 json helper function to help me parse any json. One convert json to dictionary. Another parse json into array. In fact, the whole json, is either dictionary (nested or not nested) or array (nested or not nested). Am I correct with this one?
Now the codes are:
Public Shared Function jObjectToDictionary(json As String) As Generic.Dictionary(Of String, String)
Dim json1 = JObject.Parse(json)
Dim dict = json1.ToObject(Of Generic.Dictionary(Of String, Object))
Dim dict2 = dict.ToDictionary(Function(x) x.Key, Function(x) x.Value.ToString)
Return dict2
End Function
Public Shared Function jObjectToArray(json As String) As Generic.List(Of String)
Dim json2 = JObject.Parse(json)
Dim ar = json2.ToObject(Of Generic.List(Of Object))
Dim ar2 = ar.ConvertAll(Function(x) x.ToString)
Return ar2
End Function
The code sort of works.
I then did
Dim jsonar = jsonHelper.jObjectToDictionary(_jsonResult)(starttoken) 'works
Dim ar1 = jsonHelper.jObjectToArray(jsonar) 'doesn't work
Then I examine why jObjectToArray doesn't work and it leads to this one line code that I think should work but doesn't.
That's what I ask. If you can fix this problem of that one line code, I can figure the rest out.
The reason that JObject.Parse is failing is because your JSON represents an array of objects, not just a single object. You can tell because the JSON begins and ends with square brackets [ and ], as opposed to curly braces { and } which denote a single object. To parse an array, you need to use JArray.Parse. (Note you can also use JToken.Parse -- that will parse either a single object or an array.)
Dim json2 = Newtonsoft.Json.Linq.JArray.Parse("[{""currency_code"":""1ST"",""cash"":""0"",""reserved"":""0""},{""currency_code"":""8BT"",""cash"":""0"",""reserved"":""0""}]")
Demo fiddle: https://dotnetfiddle.net/hZlc3m
I have been trying to serialize some json data in Silverlight. I am using the following code
System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(stacks.GetType());
MemoryStream ms = new MemoryStream();
serializer.WriteObject(ms, stacks);
StreamReader reader = new StreamReader(ms);
string json = reader.ReadToEnd();
to attempt the serialization. It does not work. It was the only example I could find that did not produce errors in Visual Studio. I am passing a list of custom coded objects (stacks). When I try to view the results I am getting a blank string. Anyone got some ideas on how to point me in the right direction?
The stream cursor is pointing to the end (after everything was written). Add the line "ms.Position = 0;" before creating the StreamReader.