-
Notifications
You must be signed in to change notification settings - Fork 3
/
Pretty JSON
44 lines (42 loc) · 1.07 KB
/
Pretty JSON
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
Pretty JSON:-
Difficulty = Medium
#include <bits/stdc++.h>
vector<string> prettyJSON(string &str) {
int n=str.size(),space=0;
vector<string> ans;
string s="";
for(int i=0;i<n;i++){
if(str[i]=='[' || str[i]=='{'){
if(s!=""){
string aa="";
for(int j=1;j<=space;j++) aa+="\t";
ans.push_back(aa+s);
}
string aa="";
for(int j=1;j<=space;j++) aa+="\t";
ans.push_back(aa+str[i]);
space++;
s="";
}
else if(str[i]==','){
s+=str[i];
string aa="";
for(int j=1;j<=space;j++) aa+="\t";
ans.push_back(aa+s);
s="";
}
else if(str[i]==']'|| str[i]=='}'){
if(s!=""){
string aa="";
for(int j=1;j<=space;j++) aa+="\t";
ans.push_back(aa+s);
}
space--;
s=str[i];
}
else s+=str[i];
}
ans.push_back(s);
return ans;
// Write your code here.
}