0

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

bluewonder
  • 41
  • 2
  • 7
  • 2
    NO, You will have to write some own custom function, how will any other third party function know where to split your plain text string – Vinay Pratap Singh Bhadauria Jun 19 '14 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 Jun 19 '14 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 Jun 19 '14 at 12:14
  • Are you looking a solution in c# or javascript? – L.B Jun 19 '14 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 Thanks – bluewonder Jun 19 '14 at 12:27

1 Answers1

2
 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"} 
L.B
  • 114,136
  • 19
  • 178
  • 224