typedef struct _json
{
char * Json_name;//数据名称
char * Json_buf;//数据
UINT name_len;
UINT buf_len;
_json * next;
UINT num;
}Json;
Json * Json_Insert(Json * json,char * name,int name_len,char * buf,int buf_len)
{
if (name)
{
json->Json_name=(char *)malloc(name_len + 1);
ZeroMemory(json->Json_name,name_len + 1);
MoveMemory(json->Json_name,name,name_len);
json->name_len=name_len;
}
else
{
json->Json_name=NULL;
json->name_len=0;
}
if (buf)
{
json->Json_buf=(char *)malloc(buf_len + 1);
ZeroMemory(json->Json_buf,buf_len + 1);
MoveMemory(json->Json_buf,buf,buf_len);
json->buf_len=buf_len;
}else
{
json->Json_buf=NULL;
json->buf_len=0;
}
json->next=new Json;
json=json->next;
json->next=NULL;//将下一个链表的next 指向 NULL
return json;
}
Json * Json_read(char * buf_temp)
{
char * buf=buf_temp , *m_buf =buf;
Json * json=new Json, *m_json=json;
char * Json_name=NULL,*Json_buf=NULL;
char *start =NULL,*end=NULL;
int name_len =0,buf_len =0;
while (*m_buf)//遍历内存;
{
switch(*m_buf)
{
case '"'://引号 是name 或 汉字的buf
++m_buf;
if (*m_buf == ':')
end=m_buf -1;
else if(!start)//必须为空
start=m_buf -1;
--m_buf;
break;
case ',':
if (start && end)
{
name_len = end -start;
Json_name=(char *)malloc(name_len);
ZeroMemory(Json_name,name_len);
MoveMemory(Json_name,start + 1,name_len - 1);
buf_len=m_buf-end - 1;
Json_buf=(char *)malloc(buf_len);
ZeroMemory(Json_buf,buf_len);
MoveMemory(Json_buf,end + 2,buf_len -1);
m_json=Json_Insert(m_json,Json_name,name_len -1,Json_buf,buf_len - 1);
free(Json_name),free(Json_buf);//清理
start=NULL,end=NULL;
}
case '{'://类开始
if (start && end)
{
name_len = end -start;
Json_name=(char *)malloc(name_len);
ZeroMemory(Json_name,name_len);
MoveMemory(Json_name,start + 1,name_len - 1);
m_json=Json_Insert(m_json,Json_name,name_len -1,NULL,0);
free(Json_name);//清理
start=NULL,end=NULL;
}
break;
case '}'://类结束;
if (start && end)
{
name_len = end -start;
Json_name=(char *)malloc(name_len);
ZeroMemory(Json_name,name_len);
MoveMemory(Json_name,start + 1,name_len - 1);
buf_len=m_buf-end - 1;
Json_buf=(char *)malloc(buf_len);
ZeroMemory(Json_buf,buf_len);
MoveMemory(Json_buf,end + 2,buf_len -1);
m_json=Json_Insert(m_json,Json_name,name_len -1,Json_buf,buf_len - 1);
free(Json_name),free(Json_buf);//清理
start=NULL,end=NULL;
}
break;
}
++m_buf;
}
/*
while (json->next)
{
if (json->Json_buf)
{
MessageBoxA(NULL,json->Json_buf,json->Json_name,MB_OK);
}else
MessageBoxA(NULL,json->Json_name,"类名",MB_OK);
json =json->next;
}*/
return json;
}