对json字符串的处理

2025-11-12 18:33:00

1、一下就是头文件所有内容:

/************json.h********************************/

#include<iostream>

#include<cstring>

using namespace std;

int  changetoint(string str);//change string data to int type

string gitemv(string str,string item);//get the item value from json string

2、测试的程序也很简单:

/***********************main.cpp*****************************/

#include "json.h"

int main()

{

 string str="{ \"DId\":\"154-155\", \"power \": \"1\"}";

 cout<<str<<endl;

 string temp=gitemv(str,"power ");

cout<<temp<<endl;

int command=changetoint(temp);

cout<<command<<endl;

return 0;

}

测试结果:

{ “DId":"154-155","power":"1"}

1

1

所以,子项(item ) power 的值(value) 是 1,

把字符串”1“,转成整型 (command)数字 1

3、当然,必不可缺的是实现功能的CPP源文件了:

/***********************json.cpp**************************/

#include "json.h"

int changetoint(string str)

{

  int len=str.length();

  int value=0;

 for(int i=0;i<len;i++)

                if(str[i] != ' '&&str[i]>47&&str[i]<58)value=value*10+(str[i]-48);

 return value;

}

//以下是个辅助函数

void changestring(string &item)

{

 int len=str.length();

 char temp4[200]={'\0'};

 char temp5[200]={'\0'};

 int start1=0;

 int end1=0;

 int y1=0;

 for(int l=0;l<len;l++)tem4[l]=item[l];

 for(int r=0;r<len;r++){

            if(temp4[r] != ' '){

                    start1=r;

                    break;

              }

 }

 for(int y=len-1;y>start1;y--){

              if(temp4[y] != ' '){

                       end1=y;

                       break;

              }

 }

 for(int r1=start1;r1<=end1;r1++){

                   temp5[y1]=temp4[r1];

                   y1++;

  }

 temp5[y1]='\0';

 item=temp5;

 }

4、//以下是主要的function了

string gitemv(string str,string item)

{

 int j=0;k=0;g=0;start=0;end=0;position=0;

 char temp[200]={'\0'};

 char temp1[200]={'\0'};

 string result;

 changestring(item);

 int length=str.length();

 int len= item.length();

 for(int s=0;s<length;s++){

             if(str[s]==item[0]){

                       int s1=0;

                       for(;s1<len;s1++)

                                     if(item[s1] != str[s+s1])g=1;

                       if(g==0){

                                int temp2=s+s1;

                                int sw=str.find('"',temp2);

                                if(temp2==sw)position=sw;

                                for(int i=temp2;i<sw;i++){

                                      if(str[i]!=' ')break;

                                      if(i==sw-1)position=sw;

                                 }

                        }

                        g=0;

              }

 }

 int position1=str.find(':',position);

 int position2=str.find(',',position1);

 if(position2==-1){

            position2=str.find('\n',position1);

            if(position2==-1)position2=str.find('}',position1);

            str[position2]='%';

 }

 else str[position2]='%';

 int position3=str.find('%',position1);

 for(int i4=position1+1;i4<position3;i4++){

             if(str[i4] != '"'){

                        temp[k]=str[i4];

                        k++;

             }

 }

 for(int i5=0;i5<k;i5++){

              if(temp[i5]!= ' '){

                         start=i5;

                         break;

              }

 }

for(int i6=k-1;i6>=start;i6--){

              if(temp[i6]!= ' '){

                         end=i6;

                         break;

              }

 }

for(int i7=start;i7<=end;i7++){

              temp1[g]=temp[i7];

               g++;

 }

 result=temp1;

return result;

}

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢