Search code examples
cjsonserializationjson-serialization

JSON serialization of C char array (char*)


Is it possible to encode a C char array (char*) in JSON in order to send it through a TCP socket? If so how can I make it and which would be a simple JSON library to use?

Thanks,


Solution

  • You probably don't need to use JSON. If you just want to send a simple message (e.g. "Hi, how are ya"), then you can just send the char array. You cannot send JSON, only a char array of JSON (JSON is a data format, not a transfer protocol).
    Example

    const char *json = "{\"id\": 12837, \"name\": \"Massimo Isonni\"}"
    // You would then send json.
    

    If you need to serialize a struct into a JSON string, I would recommend this.