0

I have a String like this :

var str = [sa_user{username=pankaj123, userType=dataentry}, sa_user{username=data, userType=dataentry}, sa_user{username=PANKAJ, userType=parcel}, sa_user{username=davender, userType=parcel}, sa_user{username=delhiparcel, userType=dataentry}, sa_user{username=devender, userType=dataentry}, sa_user{username=amit123, userType=dataentry}, sa_user{username=sanjay, userType=dataentry}, sa_user{username=MUKESH, userType=dataentry}, sa_user{username=test, userType=dataentry}, sa_user{username=vijaykumar, userType=dataentry}, sa_user{username=puran123, userType=dataentry}, sa_user{username=sanjaykumar, userType=dataentry}, sa_user{username=nelsonanthony, userType=dataentry}, sa_user{username=ishan, userType=dataentry}, sa_user{username=manoj, userType=parcel}, sa_user{username=ranjeet, userType=dataentry}, sa_user{username=DEEPAK, userType=dataentry}, sa_user{username=ASHISH, userType=dataentry}, sa_user{username=arjun, userType=dataentry}]

I want to convert the given into JSon Object or any other data-type to access every Object variables username and usertype.

Since, the String is not in the valid form to convert into JSon Object, then how can I access the variables?

3
  • You don't have a string. Should what is being assigned to str be enclosed in quotes? Commented Feb 18, 2014 at 10:06
  • I've got this String from server, not initialized it here, this is just an example...@Graham Commented Feb 18, 2014 at 10:09
  • convert to json string at server, can't you convert to json string using JavaScriptSerializer (in .netframework if you are using that) before sending to client ? Commented Feb 18, 2014 at 10:29

2 Answers 2

1

You need to parse the string, convert it to a valid json string and then in javascript you can do something like this to get the object:

var myJsonString = ""; // your json string
var obj = JSON.parse(myJsonString); 

EDIT: Just put together a jsfiddle with a solution, it could be better, but it gets the job done for your specific case.

http://jsfiddle.net/8B7wu/1/

Sign up to request clarification or add additional context in comments.

2 Comments

Let me get a jsfiddle with the answer.
@AnkitLamba just edited the answer with a solution for your specific case. Please Review it.
1

your json string should look like this to parse.

var strJson = "[
                   {'username':'pankaj123', 'userType':'dataentry'}, 
                   {'username':'data', 'userType':'dataentry'}, 
                   {'username':'PANKAJ', userType':parcel'}, 
                   ....
               ]";

now solve what to do with your current string to make it like this.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.