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

5
  • 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 Commented 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? Commented 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. Commented Jun 19, 2014 at 12:14
  • Are you looking a solution in c# or javascript? Commented 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 Thanks Commented Jun 19, 2014 at 12:27

1 Answer 1

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"} 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.