I have one text string as following :
workingtable;AB8C;book_id;7541;
I would like to convert them into JSON format like : {"workingtable":"AB8C","book_id":"7541"}
Is there any JSON function so that I can convert the raw text string to JSON format like that in Javascript?
Thanks
asked Jun 19, 2014 at 12:04
bluewonder
431 gold badge2 silver badges7 bronze badges
-
2NO, You will have to write some own custom function, how will any other third party function know where to split your plain text stringVinay Pratap Singh Bhadauria– Vinay Pratap Singh Bhadauria2014年06月19日 12:07:33 +00:00Commented Jun 19, 2014 at 12:07
-
@Rex : Thanks, I can separate them by the semi colon ; so should we have the function stringify to convert them after splitting?bluewonder– bluewonder2014年06月19日 12:13:07 +00:00Commented Jun 19, 2014 at 12:13
-
What you should do is create a class that will contain those fields and then use a real JSON library like JSON.Net to serialize your object to its JSON representation.Bun– Bun2014年06月19日 12:14:53 +00:00Commented Jun 19, 2014 at 12:14
-
Are you looking a solution in c# or javascript?L.B– L.B2014年06月19日 12:23:26 +00:00Commented Jun 19, 2014 at 12:23
-
@L.B : Hi Im looking for the solution in Javascript the data exists in the input text field which is separated by semicolon Thanksbluewonder– bluewonder2014年06月19日 12:27:49 +00:00Commented Jun 19, 2014 at 12:27
1 Answer 1
var s = "workingtable;AB8C;book_id;7541;";
var parts = s.split(';');
var jobj = {};
for(i=0;i<parts.length;i+=2)
{
jobj[parts[i]]=parts[i+1];
}
alert(JSON.stringify(jobj));
OUTPUT:
{"workingtable":"AB8C","book_id":"7541"}
answered Jun 19, 2014 at 12:45
L.B
116k20 gold badges189 silver badges229 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-js