0

I am making a jquery ajax call to the following text file:

sport=Table Tennis&groups=no&no_groups=&grp_names=&teams=1^China^6157~2^Finland^6158~3^Sweden^6159~4^Japan^6149~5^Korea^6154&Endstr=End

The call is working fine. But I really don't know how to access a particular value like lets say, 'China' from the above text file? I am trying tis for the first time. please help..

1
  • "China" does not seem to have a value, it’s just a part of the team string. Commented Dec 12, 2012 at 9:48

3 Answers 3

1

Here is the parseParams plugin which you can use to split a querystring into an array.

You can use it like this:

var querystring = 'sport=Table Tennis&groups=no&no_groups=&grp_names=&teams=1^China^6157~2^Finland^6158~3^Sweden^6159~4^Japan^6149~5^Korea^6154&Endstr=End';
var paramsObj = $.parseParams(querystring);
alert(paramsObj.sport); // = Table Tennis
Sign up to request clarification or add additional context in comments.

3 Comments

thanks its working..but still i cant access a particular value like china as i said earlier.. alert(paramsObj.teams); will display the entire teams string..
That's correct. China is part of the value - which could conceivably change. You would need to split the value to get the part you want.
And that's why people invented JSON :D
1

Don't use such file structure. Use JSON instead. i.e:

{
   "sport":"Table Tennis",
   "groups":false,
   "no_groups":"",
   "grp_names":[

   ],
   "teams":[
      {
         "China":6157
      },
      {
         "Finland":6158
      },
      {
         "Sweden":6159
      },
      {
         "Japan":6149
      },
      {
         "Korea":6154
      }
   ],
   "Endstr":"End"
}

Then, after you parse it with $.get or $.ajax, you just access:

data.sports

for example.

Comments

0

Here is code to parse your data. It would have been straight forward if you have chosen JSON as return data.

function myParser() {

    var str = "sport=Table Tennis&groups=no&no_groups=&grp_names=&teams=1^China^6157~2^Finland^6158~3^Sweden^6159~4^Japan^6149~5^Korea^6154&Endstr=End";
    var arPairs = str.split("&");
    var outObj = {};
    for (var i=0; i < arPairs.length; i++) {
      var arTemp = arPairs[i].split("=");
      var key = arTemp[0];
      var value = arTemp[1];
      if (key === "teams") {
        arTemp = value.split("~");
        var teamObj = {};
        for (var j=0; j < arTemp.length; j++) {
          var arTeamData = arTemp[j].split("^");
          teamObj[arTeamData[1]] = arTeamData[2];
        }
        value = teamObj;
      }
      outObj[key] = value;
    }
    return outObj;
}

output: Is an array having all your data available. You can refer it as:

var outObj = myParser(); console.log(outObj.sport); // Prints "Table Tennis" console.log(outObj.teams.china) // Prints china data

3 Comments

thanks..but as you said it prints the china data..i.e. 6157 and not the name 'china'..any way i can do it?
Yes, just do for (var teamName in outObj.teams) {
You can also change my code (teamObj[arTeamData[1]] = arTeamData[2];) to insert into an array if you wish. I used object for quick reference.

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.