Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
json formatter
#include <stdio.h> // printf #include <string.h> // strlen, strcat, strncat #include <stdlib.h> // malloc extern char * str2JsonFormat(char *prevStr, char *key, char *valueStr); // =========================================================================================================================== // ======== JSON formatter ========== // =========================================================================================================================== // Takes three parameters // 1) 'prevStr' string. If empty, creates a default new JSON formated string. Otherwise adds a ',' (comma) // 2) 'key' property parameter // 3) 'valueStr' string parmeter. Can be a character, string, integer or float viewed as a string type. // NOTE: This will NOT convert integers and floats to strings. It does not know how many decimal places to convert. // This will need to be done before passing as a string parameter. // Then wraps it in JSON ["key": "value"] string type format and appends it to the previous string and returns the new string. // Each ["key": "value"] pair is seperated by a "," (comma) // // External Library funtions: // strlen, strncat, strcat, malloc // // Usage: // exampleOutput = str2JsonFormat(lastString, "name", "data"); // // Example1 output: (if last string was empty) // {"name":"data"} // Example2 output: (if last string was NOT empty) // {"name":"data","name":"data"} // char * str2JsonFormat(char *prevStr, char *key, char *valueStr) { char *out; // formatted JSON return string int outSize = 0; // size of formatted JSON return string int overhead = 8; // extra characters for JSON formatting // need to calculate space needed for formatted return string // Add length of previous string input parameter // Add length key string input parameter // Add length valueStr string input parameter // Add overhead characters to reflect JSON formatting outSize = strlen(prevStr) + strlen(key) + strlen(valueStr) + overhead; // need to malloc memory location out = (char *) malloc(sizeof(char) * outSize); // if previous string is empty ceate default string if ( strlen(prevStr) == 0) { strcat(out, "{"); // add a "{" (left brace) } // otherwise append previous string to formatted return string with slight modifications else { strncat(out, prevStr, strlen(prevStr)-1 ); // delete the last '}' (right brace) strcat(out, ","); // and add ',' (comma) } // concatenate new key / value pair with JSON formatting strcat(out, "\""); // add a '"' (double quote) strcat(out, key); strcat(out, "\":\""); // complete the (double quote), add ':' (colon), start a new (double quote) strcat(out, valueStr); strcat(out, "\"}"); // add a '}' (right brace) // return the JSON formatted string return out; } int main() { char lastString[100]; char * exampleOutput; // strcat(lastString, "\0"); strcat(lastString, "{\"name1\":\"data\"}\0"); exampleOutput = str2JsonFormat(lastString, "name2", "data"); printf(" output length is %li \r\n", strlen(exampleOutput) ); printf(" output is %s \r\n", exampleOutput); return 0; }
run
|
edit
|
history
|
help
0
void *
Access the structure variable
No Pyramid toggle
data types and printf
modify char by mem addr
Hello
B_141202_CADENA_NUMERO
B_141121_aleatorios
main.c
test