删除json格式文件中的双引号

时间:2018-01-22 20:23:00

标签: javascript json

我正在尝试将下面的JSON格式文件转换为下面提到的输出格式(我基本上试图在javascript中删除像GroupId这样的属性名称中的双引号)。有人对此有任何建议吗?

输入格式

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <assert.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <dirent.h>
#include <pthread.h>
#include <signal.h>

static volatile int keepRunning = 1;
int pcc_total[159];

void intHandler(int dummy) {
    keepRunning = 0;
}

char* readMsg(int sockfd, size_t *msgSize)
{
    *msgSize = 0;

    unsigned int length = 0;
    int bytes_read = read(sockfd, &length, sizeof(length)); //Receive number of bytes
    if (bytes_read <= 0) {
        perror("Error in receiving message from server\n");
        return NULL;
    }
    length = ntohl(length);

    char *buffer = malloc(length+1);
    if (!buffer) {
        perror("Error in allocating memory to receive message from server\n");
        return NULL;
    }

    char *pbuf = buffer;
    unsigned int buflen = length;
    while (buflen > 0) {
        bytes_read = read(sockfd, pbuf, buflen); // Receive bytes
        if (bytes_read <= 0) {
            perror("Error in receiving message from server\n");
            free(buffer);
            return NULL;
        }
        pbuf += bytes_read;
        buflen -= bytes_read;
    }

    *msgSize = length;
    return buffer;
}

int sendMsg(int sockfd, char *msg, size_t msgSize)
{
    unsigned int convertedNum = htonl(msgSize);
    if (write(sockfd, &convertedNum, sizeof(convertedNum)) < 0) { //Send number of bytes
        perror("error writing to socket");
        return -1;
    }

    if (write(sockfd, msg, msgSize) < 0) { //Send bytes
        perror("error writing to socket");
        return -1;
    }

    return 0;
}

void *compute(void *socket_desc) {
    int sock = * (int*) socket_desc;
    free(socket_desc);

    if (sendMsg(sock, "hi", 2) != 0) {
        perror("error writing to socket\n");
        close(sock);
        return NULL;
    }

    size_t length = 0;
    char *data = readMsg(sock, &length);
    if (!msg) {
        close(sock);
        return NULL;
    }
    printf("len is %d\n", (int)length);
    printf("data is %.*s\n", (int)length, data);

    int count = 0;
    for (size_t i = 0; i < length; i++) {
        // ...
    }
    free(data);

    char scount[20];
    sprintf(scount, "%d", count);
    sendMsg(sock, scount, strlen(scount));

    close(sock);
    return NULL;
}

int main(int argc, char *argv[]) {

    unsigned int port = atoi(argv[1]);
    signal(SIGINT, intHandler);

    int socket_desc, client_sock, c, *new_sock;
    struct sockaddr_in server, client;

    socket_desc = socket( AF_INET, SOCK_STREAM, 0);
    if (socket_desc == -1) {
        perror("Could not create server socket");
        return 1;
    }
    printf("server socket created\n");

    memset(&server, 0, c);
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = htonl(INADDR_ANY);
    server.sin_port = htons(port);

    if (bind(socket_desc, (struct sockaddr*) &server, sizeof(server)) < 0) {
        perror("\n Error : Bind Failed \n");
        close(socket_desc);
        return 1;
    }
    printf("bind created\n");

    if (listen(socket_desc, 10) < 0) {
        perror("\n Error : Listen Failed \n");
        close(socket_desc);
        return 1;
    }
    printf("listening\n");

    while (keepRunning) {
        c = sizeof(client);
        client_sock = accept(socket_desc, (struct sockaddr *) &client,
                            (socklen_t*) &c);
        if (client_sock < 0) {
            perror("\n Error : Accept Failed\n");
            continue;
        }
        printf("client accepted\n");

        new_sock = malloc(sizeof(int));
        if (!new_sock) {
            perror("\n Error : Malloc Failed\n");
            close(client_sock);
            continue;
        }
        *new_sock = client_sock;

        pthread_t tid;
        if (pthread_create(&tid, NULL, &compute, new_sock) != 0) {
            perror("\n Error : Thread Create Failed\n");
            free(new_sock);
            close(client_sock);
            continue;
        }
        printf("thread created\n");
    }

    close(socket_desc);
    return 0;
}

输出格式

{ 
  "GroupId": "sg-1234556", 
  "GroupName": "sggroupname",
  "IpPermissions": [ 
   {             
     "FromPort": 9090, 
     "ToPort": 9090, 
     "IpProtocol": "tcp", 
     "IpRanges": [ { "CidrIp": "0.0.0.0/0" } ] 
 }]
}

1 个答案:

答案 0 :(得分:0)

使用regexp

var object = {
  "GroupId": "sg-1234556",
  "GroupName": "sggroupname",
  "IpPermissions": [{
    "FromPort": 9090,
    "ToPort": 9090,
    "IpProtocol": "tcp",
    "IpRanges": [{
      "CidrIp": "0.0.0.0/0"
    }]
  }]
};

var regexp = /"(\w+)"\s*:/g;
var group = '$1:';

var result = JSON.stringify(object, null, 2).replace(regexp, group);

console.log(result);