Json formatting issues


#1

Hi all - I’ve been working on a new html template (moving over from flash). so looking to stream some json data over to the server in realtime.

using json. net I have been running into the unexpected end of JSON input error.

testing with the client I’ve noticed that the server string received includes \ escapes for each quote symbol used.

So two questions;

  • does the json data send require the \ escapes?
  • if yes does anyone know how these can be added using json. net framework?

Cheers

Dan


#2

I use the Newtonsoft.Json.dll library for converting my data from dataset to JSON:

 jSet.DateFormatString = "dd.MM.yyyy"
 jSet.Formatting = Newtonsoft.Json.Formatting.None
 mCSData = JsonConvert.SerializeObject(mds, jSet)
 mCSData = """" + mCSData.Replace("""", "\""") + """"

mds is my dataset, mCSData is a string which I send to Caspar via CG ADD command.

J


#3

thanks Jarda, i’m doing the same;

 Dim output_data_json As String = JsonConvert.SerializeObject(out_obj, Formatting.None, New JsonSerializerSettings With {.NullValueHandling = NullValueHandling.Ignore})
   
 Dim output_data As String = String.Format("CG 1-20 ADD 1 {0} 1 ""{1}""", """TEST-1/SAMPLE1""", output_data_json)
       

    CasperLink.SendString(output_data)

which gives me;

[2019-04-05 15:30:08.367] [info] Received message from 192.168.1.5: CG 1-20 ADD 1 "TEST-1/SAMPLE1" 1 "{"f0":"RACE001 00:59.8","p1":"1","n1":"Lane_2","d1":"0"}"\r\n [2019-04-05 15:30:08.368] [info] Sent message to 192.168.1.5:202 CG OK\r\n [2019-04-05 15:30:08.373] [info] html[file://C:/Users/threadripper/Documents/casparcg-server-2.2.0-stable-windows/template/TEST-1/SAMPLE1.html] Destroyed. [2019-04-05 15:30:08.461] [info] html[file://C:/Users/threadripper/Documents/casparcg-server-2.2.0-stable-windows/template/TEST-1/SAMPLE1.html] 1920 1080 50 Log: Uncaught SyntaxError: Unexpected end of JSON input


#4
mCSData = """" + mCSData.Replace("""", "\""") + """"

The result seems like:
CG 1-100 ADD 1 "ENTRY01" 0 "{\"scalar\":[{\"id\":\".b-home\",\"par\":\"background-color\",\"val\":\"rgb(235, 235, 235)\"},{\"id\":\".b-away\",\"par\":\"background-color\",\"val\":\"rgb(0, 10, 120)\"},...etc


#5

You need to put it into a code block for it to preserve formatting like you want


#6

Do You meen a preformatted text?
Sorry for my stupid question, but I vainly seeking “CODE” in the menu…:frowning:


#7

You can write code blocks by opening and closing your code snippet with a triple backtick ```.

{
  title: "This is a test code block",
  subtitle: "you can write whatever you want",
  content: [
    "you can",
    "even paste",
    "tabbed",
    "text"
  ]
}

Check this cheatsheet for more tips on writing on the forum. :wink:


#8

Many thanks Jarda,

that seems to do the trick. I actually found the value within the key:pair would replace to
{\"foo\":\"bar\\\}"
so also added a second replace to fix that;
string.replace("\\\","\")

Not sure how that is happening compaired to your output - all key:pairs are strings.