-2

Please tell me how to parse this string which is coming from a webservice as a map.

when i using alert in my javascript i am getting the response as

{1=xxx,2=eee,3=ddd,4=rrr,D=3,R=ttt}

I want to get the values before the D key..

as {"1":"xxx","2":"eee","3":"ddd","4":"rrr"}

enter code here

and also an alert for D value as 3

and also an alert for R value as ttt

Thanks in advance

Frogmouth
  • 1,808
  • 1
  • 14
  • 22
Aj.
  • 27
  • 1
  • 7

3 Answers3

0

What you've got there is almost valid JSON (JavaScript Object Notation), but not quite.

JSON requires that the property names be double-quoted, for example. To JSON-stringify a TreeMap, you could try starting here: How to serialize a Map of a Map with GSON?

Or you could roll your own JSON stringification, if you can't get GSON working: Double quote the property names, double quote string values, make sure special characters in string values are properly escaped (backslash becomes \\, newline \n, double quote becomes \", etc.). I'd try GSON first.

That's all in the webservice. Now, on the client, you'll have to turn this string into a JavaScript object.

Probably the simplest way is to explicitly parse it on the client with JSON.parse(). For that, the answer is here: How to Parse JSON in JavaScript.

Some will advise you to set the webservice's ContentType to application/json and let the thingy parse it. Fair enough, but this'll work fine.

Community
  • 1
  • 1
0

You are doing double work. Not sure why you would want this rather than to format it as json on the server, and just ajax this to the client formatted as json. But anyway, just to answer your question DEMO:

 // your stirng '{1=xxx,2=eee,3=ddd,4=rrr,D=3,R=ttt}';
 var MyObjects = [];
 //And here is how you use it:
  MyObjects = ArrayOfObjects.get('{1=xxx,2=eee,3=ddd,4=rrr,D=3,R=ttt}', 'D=3');
  //{"1":"xxx","2":"eee","3":"ddd","4":"rrr"}
  console.log(MyObjects[0]);
  //Your alerts you needed
  console.log(MyObjects[1].D, MyObjects[1].R);

 //Function you need
 var ArrayOfObjects = (function(){
   var splitOn = function(str, char){
   var arr = [], pos = str.indexOf(char);
   arr.push(str.slice(1, (pos-1)));
   arr.push(str.slice(pos, (str.length - 1)));
  return arr;
 },
 getObj = function (str){
   var strNew = str.replace(new RegExp('=', 'g'), ':');
   var jsonSTR = strNew.replace(/(\w+)|(\d+)/g, '"$1"');
   return JSON.parse("{"+jsonSTR+"}");
  };

 return {
    get: function(string, spl){
        var strings=splitOn(string, spl), retArr = [];
         strings.forEach(function(str){
         retArr.push(getObj(str));
      });
         return( retArr );
   }
 };
})();

Again This is just an answer to your question, not the solution. The solution would be to form a correct JSON on server side.

Alex Shilman
  • 1,547
  • 1
  • 11
  • 15
  • yes,you are on my way ..can you give me the obj which consists key values before 'D' ..like '{"1":"xxx","2":"eee","3":"ddd","4":"rrr"}'..dont want the values 'D=3 and R = ttt – Aj. Jan 13 '14 at 14:19
  • But again, this is just an answer to your question, but it is not the solution. The solution would be to have a server return json. – Alex Shilman Jan 13 '14 at 16:02
  • Let me give my requirement the values which i have in javascript by using those values i have to set to a select drop down box..but i am unable to do that..i have to form a select drop down by using the values only till before 'D' and '3' where D is key and 3 is values like wise 1 is key and xxx is value so on.. – Aj. Jan 13 '14 at 17:10
  • Do it like this using my example: MyObjects = ArrayOfObjects.get('{1=xxx,2=eee,3=ddd,4=rrr,D=3,R=ttt}', 'D=3'); //{"1":"xxx","2":"eee","3":"ddd","4":"rrr"} console.log(MyObjects[0]); – Alex Shilman Jan 13 '14 at 17:25
  • Is this what you want? – Alex Shilman Jan 13 '14 at 17:57
-1

You can do something like this:

 (function(r,x,s,a,k){
    r = {};
    s = "{1=xxx,2=eee,3=ddd,4=rrr,D=3,R=ttt}";
    a = s.replace(/[{|}]/g, "").split(",");
    for(k = 0; k < a.length; k++){
        x = a[k].split("=");
        if(x[0] == "D" || x[0] == "R") alert(x[1]); 
        else r[x[0]] = x[1];
    }
 })();

res are an object:

{
 1 : xxx,
 2 : eee,
 3 : ddd,
 4 : rrr
}

but it seems useless... maybe is better to return a JSON form your service API.

Maybe you can do the same with regexp.

Frogmouth
  • 1,808
  • 1
  • 14
  • 22
  • How can i send Json object from my java webservice ..please help – Aj. Jan 13 '14 at 14:04
  • @user3185790 JSON is a string. It looks like what you're already sending. You parse it on the client: [How to Parse JSON in JavaScript](http://stackoverflow.com/questions/4935632/how-to-parse-json-in-javascript). – 15ee8f99-57ff-4f92-890c-b56153 Jan 13 '14 at 14:06
  • ... sorry but i'm not a JAVA programmer... but i think you need to get back to your client a string with conentType: Application/Json... see this aswer: http://stackoverflow.com/questions/2010990/how-do-you-return-a-json-object-from-a-java-servlet – Frogmouth Jan 13 '14 at 14:08
  • 1
    @Frogmouth This is a truly awful answer. You parse JSON with a JSON parser, not with some flimsy regex contraption, or worse yet the replace/split monstrosity you've got here. I mean, this isn't even the right *wrong* way to do it. – 15ee8f99-57ff-4f92-890c-b56153 Jan 13 '14 at 14:08
  • ... but the returned string isn't a JSON. So... if the service get back a JSON string JSON parser is the way. – Frogmouth Jan 13 '14 at 14:09
  • @EdPlunkett Please see the above comments..i am converting TreeMap to string and returing from server..how can i return JSON instead of string in Axis2 webservice.. – Aj. Jan 13 '14 at 14:12