3

My application receives a JSon message via UDP from coordinates of an object as follows:

{
    "FRAME1": {
        "X": "-0.885498",
        "Y": "-0.205078",
        "Z": "0.628174"
    },
    "FRAME2": {
        "X": "1.70898",
        "Y": "-5.67627",
        "Z": "0.305176"
    },
    "ID": "DEVICE9",
    "TS": "7.9900"
}

I need to read the coordinates from FRAME1 and FRAME2 (X,Y,Z values of each frame). The Json message is stored in char[] msg. I use these the following lines of code two get the coordinates of FRAME1:

char Xc[32], Yc[32], Zc[32];
sscanf (msg,
        "{\"X\":\"%s\",\"Y\":\"%s\",\"Z\":\"%s\"}",
        Xc, Yc, Zc);

I display the stored value with printf as:

printf("X coordinate is: %s\n" , Xc);

The output, however is strange:

X coordinate is: |-

What is wrong in the format provided for sscanf()?

8
  • Use some JSON library but not sscanf. Commented Feb 19, 2016 at 10:57
  • Do you check the return value from sscanf? Maybe you should! Commented Feb 19, 2016 at 10:59
  • @Michael Walz: Do you suggest anything in specific? Commented Feb 19, 2016 at 10:59
  • Shouldn't the sscanf format string contain some whitespace between the colons and the double quotes? Commented Feb 19, 2016 at 11:02
  • @QuestionMark SimpleJSON is quite good. Commented Feb 19, 2016 at 11:04

2 Answers 2

3

sscanf with the %n specifier and strcmp could be used to parse the string. %n captures the number of characters used in the scan.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
    int index = 0;
    int offset = 0;
    char sub[300] = "";
    char Xc[32] = "";
    char Yc[32] = "";
    char Zc[32] = "";
    char msg[] = {
    "{\n\
    \"FRAME1\": {\n\
        \"X\": \"-0.885498\",\n\
        \"Y\": \"-0.205078\",\n\
        \"Z\": \"0.628174\"\n\
    },\n\
    \"FRAME2\": {\n\
        \"X\": \"1.70898\",\n\
        \"Y\": \"-5.67627\",\n\
        \"Z\": \"0.305176\"\n\
        },\n\
        \"ID\": \"DEVICE9\",\n\
        \"TS\": \"7.9900\"\n\
    }\n"
    };

    offset = 0;
    while ( ( sscanf ( msg + offset, "%299s%n", sub, &index)) == 1) {
        offset += index;
        if ( strcmp (sub, "\"X\":" ) == 0) {
            if ( ( sscanf ( msg + offset, "%31s%n", Xc, &index)) == 1) {
                offset += index;
                printf ( "Xc is %s\n", Xc);
            }
        }
        if ( strcmp (sub, "\"Y\":" ) == 0) {
            if ( ( sscanf ( msg + offset, "%31s%n", Yc, &index)) == 1) {
                offset += index;
                printf ( "Yc is %s\n", Yc);
            }
        }
        if ( strcmp (sub, "\"Z\":" ) == 0) {
            if ( ( sscanf ( msg + offset, "%31s%n", Zc, &index)) == 1) {
                offset += index;
                printf ( "Zc is %s\n", Zc);
            }
        }
    }

    return 0;
}

The above sscanf's for the coordinates will include the quotes. This could be used to remove the quotes

if ( ( sscanf ( msg + offset, " \"%31[^\"]%*s%n", Xc, &index)) == 1) {
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the snippet that compiles! In sscanf ( msg + offset, "%31s%n", Xc, &index) what is the general case if Xc, Yc and Zc have different unknown lengths?
1

Maybe you can try to use strtok() instead and use the colons as delims or something? Not sure if it makes your life any easier, but I know that strtok() is generally recommended over the *scanf() family of functions.

Comments

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.