Json Lexer - Parse Receive data

Discussion to talk about software related topics only.
Post Reply
seulater
Posts: 445
Joined: Fri Apr 25, 2008 5:26 am

Json Lexer - Parse Receive data

Post by seulater »

Looking at the examples Json, I am still confused...

I am using websockets on port 22000. When the browser sends me a json formatted string I get the data just fine.
Now I would like to parse and pull that data out. How do i put my received data into a JsonInObject so i can then use JsonInObject.FindFullNameString() or other json functions?


Using NNDK 2.7.7, MOD5270B
User avatar
pbreed
Posts: 1080
Joined: Thu Apr 24, 2008 3:58 pm

Re: Json Lexer - Parse Receive data

Post by pbreed »

So assume the data is all in a buffer pData

ParsedJsonDataSet pjs(pData,len);
or if the pData is null terminated...

ParsedJsonDataSet pjs(pData);

Then you can operate on pjs...
seulater
Posts: 445
Joined: Fri Apr 25, 2008 5:26 am

Re: Json Lexer - Parse Receive data

Post by seulater »

Thank you for the help! Tried a few ways but keep getting Trap errors.
This is what I have thus far.

Code: Select all


if (FD_ISSET(ws_fd, &read_fds))
{
    int len = read( ws_fd, rx_buffer, ETHER_BUFFER_SIZE );

    printf("RX[%d]:%s\r\n",len,rx_buffer);
                
    ParsedJsonDataSet pjs(rx_buffer,len);
    iprintf("%s\r\n",pjs.FindElementAfterName("deviceIp"));
}

The Json I am receiving is good.
RX[369]:{"CurrentConfiguration":{"unit_name":"Unit_01 ","select_static_dynamic_Ip":"true","deviceIp":"192.168.000.023","netmask":"255.255.255.000","gateway":"192.168.000.001","dns":"000.000.000.000","remoteIP":"000.000.000.000","udp_port":21800,"current_password":"ThePassowrd","alarm":"0","codec":"1","hoot_holler":"3","mode2w4w":"1","txinterval":60,"rxinterval":200}}
But this throws a trap.

Basically I want to pull each ID's data out. I.E. If I want to get the data for netmask, how do i parse the json and pull that piece out of it?
User avatar
pbreed
Posts: 1080
Joined: Thu Apr 24, 2008 3:58 pm

Re: Json Lexer - Parse Receive data

Post by pbreed »

How is rx_buffer declared?

Also this is not valid:
iprintf("%s\r\n",pjs.FindElementAfterName("deviceIp"));

pjs.FindElementAfterName returns a json_primative_type

IE an integer and your trying to print like a string?

I think you want:

iprintf("%s\r\n",pjs.FindString("deviceIp"));

or...functionally identical...

if(pjs.FindElementAfterName("deviceIp")==STRING)
{
printf("Found %s\r\n",pjs.CurrentString());
}
else
{
printf("Did not find string "deviceIp");
}
seulater
Posts: 445
Joined: Fri Apr 25, 2008 5:26 am

Re: Json Lexer - Parse Receive data

Post by seulater »

I seem to be getting close to your answers, thank you. I finally took a look at the example
C:\nburn\examples\StandardStack\WebClient\EarthQuake.

char rx_buffer[ETHER_BUFFER_SIZE + 1];
seulater
Posts: 445
Joined: Fri Apr 25, 2008 5:26 am

Re: Json Lexer - Parse Receive data

Post by seulater »

I got it the Json parsing now, thank you for your help Paul!
Post Reply