The PacketBuffer can't generate correct json for my nodejs based server

 I am using ' The PacketBuffer class' to generate the json, and send it to the server.

Now my server is a nodejs based solution, but the problem is the json that pakcetbuffer class generated can not be parsed by the nodejs server, always throwing out a   wrongsyntaxexception: unexpected token

for  example:

 

      packet.reset();                                                                                         

     packet.print("{a:");        packet.print(66);        

     packet.print(",b:");      packet.print(77);    

     packet.print("}\0");

 

Seial.println(packet.buf);      //  it prints out   {a:66,b:77},  but a correct json format should be {"a","66","b":"77"},

    ether.browseUrl(PSTR("/test?apikey="APIKEY"&data="), packet.buf, NULL, callback);   

the nodejs based server will throw out a wrongsyntaxexception: unexpected token a

I am so confused 'cause this example is similar to all the examples in github of open energy monitoring, such as the https://github.com/nathanchantrell/NanodeRF_emoncms/blob/master/NanodeRF_emoncms.ino. but this doesn't work for my nodejs based server

confused , how should I do to print a json to a nodejs based server?      

 

 

helxsz's picture

Re: The PacketBuffer can't generate correct json for my nodejs based server

Then I tried

packet.reset();

packet.print("{\"a\":\""); packet.print(66);

packet.print("\",\"b\":\""); packet.print(77);

packet.print("\"}\0");

When I Serial.println(packet.buf); // it outputs {"a","66","b":"77"}, which is what I expected

but still on the nodejs based server, it throws out a error saying wrongsyntaxexception: unexpected end of input.

i can't figure out what is wrong with the end of input.

I replace \0 with \n or \r, neither of them works

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.