2

I have used many tools such as cJSON, nxjson and jsmn parsers to parse the JSON response but none of the tools which i had used is giving the output in some structure format. Below is my JSON response in string:

{"Code":1,"MSN":0,"HWID":7001,"Data":{"SignOffRequest":{"messageId":0,"barCodeReadErrorCnt":0,"markSenseReadErrorCnt":0,"markSenseValidationErrorCnt":0,"postPrintErrorCnt":0,"custTicketFeedErrorCnt":0,"custInputTicketJamsCnt":0,"keyStrokeCnt":0,"keyStrokeErrorCnt":0,"commCrcErrorCnt":0,"readTxnCnt":0,"keyedTxnCnt":0,"ticketMotionErrorCnt":0,"blankFeedErrorCnt":0,"blankTicketJamCnt":0,"startupNormalRespCnt":0,"startupErrorRespCnt":0,"primCommMesgSentCnt":0,"commRetransmitTxnCnt":0,"rawMessage":null,"TableUpdateNo":1,"FixtureUpdateNo":0}}}

I have used cJSON tool and the output is as below which is in also a string:

{
    "Code": 1,
    "MSN":  0,
    "HWID": 7001,
    "Data": {
        "SignOffRequest":   {
            "messageId":    0,
            "barCodeReadErrorCnt":  0,
            "markSenseReadErrorCnt":    0,
            "markSenseValidationErrorCnt":  0,
            "postPrintErrorCnt":    0,
            "custTicketFeedErrorCnt":   0,
            "custInputTicketJamsCnt":   0,
            "keyStrokeCnt": 0,
            "keyStrokeErrorCnt":    0,
            "commCrcErrorCnt":  0,
            "readTxnCnt":   0,
            "keyedTxnCnt":  0,
            "ticketMotionErrorCnt": 0,
            "blankFeedErrorCnt":    0,
            "blankTicketJamCnt":    0,
            "startupNormalRespCnt": 0,
            "startupErrorRespCnt":  0,
            "primCommMesgSentCnt":  0,
            "commRetransmitTxnCnt": 0,
            "rawMessage":   null,
            "TableUpdateNo":    1,
            "FixtureUpdateNo":  0
        }
    }
}

but I don't want the output in the above format. I want the output in the form of a C structure. Is it possible to get the output in C structure?

2
  • 1
    possible duplicate of Parsing JSON using C Commented Apr 13, 2015 at 6:45
  • Closing because this is too broad. This is about parsing which is out of the scope of your understanding based on the title alone. Commented Apr 13, 2015 at 6:57

1 Answer 1

4

You need to add explicit code extracting from parsed JSON values the relevant fields, etc... This cannot be magically automated in general.

Some JSON libraries slightly facilitate this task. For instance jansson has a quite useful json_unpack function with which you could extract (in a single call) some fields from a parsed JSON value.

But it is your responsability to code the extraction and the validation of information from the JSON value, because only you can know what that JSON really means.

JSON is simply a convenient textual serialization format. It is up to you to give actual meaning to the data. It is also up to you to decide what kind of validation you want to code (at what degree do you trust the emitter of that JSON data?). If the data is coming from the Internet (e.g. AJAX queries, etc...) you should trust it as less as possible and validate it as much as possible.

Don't forget to document the meaning of the JSON data.

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

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.