0

I have some data being returned from javascript; the object looks like this:

let sched = [
    ["Sunday", 9.30, 12.00, 15.30, 22.00],
    ["Monday", 8.30, 12.00, 15.30, 19.00],
    ["Tuesday", 8.30, 12.00, 15.30, 19.00],
    ["Wednesday", 8.30, 12.00, 15.30, 19.00],
    ["Thursday", 8.30, 12.00, 15.30, 19.00],
    ["Friday",],
    ["Saturday", 8.00, 13.00]];

I'm trying to convert it into an array (2-dimensional, i think) in vb.net. Initially i thought i could use Newtonsoft JSON, but it is not well-formed. I'm doing it with a mess of string splits. Is there a better way?

     Dim splitLines = _hours.Replace("""", "").Replace(" ", "").Replace(vbCrLf, "").TrimStart("[").TrimEnd("]").Split("],")
     Dim result = splitLines.Select(Function(x) x.Trim.Replace("""", "").Replace("[", "").Split(","c)).ToArray()

Problem:

  • For each weekday i'm returning an extract object at index 0 and value of ""
  • Is there an easy way to parse something like this into a simple class?
2
  • 2
    What does _hours actually contain? A string with the let statement and all? Commented May 18, 2023 at 16:41
  • I should have clarified, _hours contains the object data as string "["Sunday.."]" Commented May 24, 2023 at 13:39

1 Answer 1

0

I think you're on the right track with splitting, if it doesn't parse correctly with JSON. But you would want to make it more regular for splitting first. Perhaps this (separate lines for clarity, but they can be nested if you prefer):

Dim splitLines As String = hours.Replace("]];", "") ' Remove last semi-colon and unnecessary endings
splitLines = splitLines.Replace("[[", "[") 'Remove first bracket
splitLines = splitLines.Replace("[""", "") 'Remove unnecessary left row divider
splitLines = splitLines.Replace(""",",";") 'Change separator after day of week to different character to parse (;)
splitLines = splitLines.Replace("],", "\") 'Change row separator to different character to parse (\)
Dim interimResult[] As String = splitLines.Split("\")
Dim result[] As String = interimResult.Select(Function(s) s.Split(";")).ToArray()

You specified a two-dimensional array, but you could split the times out by day further, if you wanted.

I referenced this for splitting into a multidimensional array: make 2 dimensional array from string split

Sign up to request clarification or add additional context in comments.

3 Comments

Why not simply deserialize this JSON? You should ask the OP to post the actual JSON object received (in the sample code, just this: ["Friday",] has an extra comma, not necessarily there when serialized)
It looked like OK JSON to me, so thought there was something I wasn't seeing. Like, as you point out, the extra comma. I assumed it was generating almost good JSON.
Correct is is almost good JSON, and sadly i cannot change the input. It does seem that none of the JSON deserializers support 'kinda-json-like'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.