-2

I am new to Json and JavaScript. Currently I have a url which returns a JSON response. But the problem is that, It is not in correct format. Please see my response below

var pd={ player: 
   { id: 363609002,
     game: 'bf4',
     plat: 'pc',
     name: 'm4jes',
     tag: 'BPt',
     dateCheck: 1487204427149,
     dateUpdate: 1474581052618,
     dateCreate: 1384980182606,
     dateStreak: 1474581052618,
     lastDay: '20160715',
     country: '',
     countryName: null,
     rank: 
      { nr: 121,
        imgLarge: 'bf4/ranks/r121.png',
        img: 'r121',
        name: 'Major General II',
        needed: 16110000,
        next: 
         { nr: 122,
           imgLarge: 'bf4/ranks/r122.png',
           img: 'r122',
           name: 'Major General III',
           needed: 16750000,
           curr: 16720600,
           relNeeded: 640000,
           relCurr: 610600,
           relProg: 95.40625 } },
     score: 16724643,
     timePlayed: 1476950,
     uId: '2832665149467333131',
     uName: 'm4jes',
     uGava: '721951facb53ed5632e196932fb6c72e',
     udCreate: 1319759388000,
     privacy: 'friends',
     blPlayer: 'http://battlelog.battlefield.com/bf4/soldier/m4jes/stats/363609002/pc/',
     blUser: 'http://battlelog.battlefield.com/bf4/user/m4jes/',
     editable: false,
     viewable: true,
     adminable: false,
     linked: false },}

from the above response I have to get the output as :

{
  game: 'bf4',
  plat: 'pc',
  name: 'm4jes',
  tag: 'BPt',
  score: 16724643,
  timePlayed: 1476950
}

By which method I can get the required out in Javascript

Sulu.MeanStack
  • 299
  • 2
  • 3
  • 18

1 Answers1

1

First of all the URL is not returning a JSON response but a JS object literal. The difference is explained here.

You have to get the string from the url. You can use the jQuery method get():

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
var jqxhr = $.get( "https://api.bf4stats.com/api/playerInfo?plat=pc&name=m4jes&output=js", function() {
  //Insert the rest of the code here
});
</script>

Then, jqxhr.responseText is the string which correspond to the object we want. With eval, we transform the string into an object:

pd = eval(jqxhr.responseText);

So here we have an object literal with all the pairs name/value. You can access the values you want by using the dot notation. If you want to get the name of the game for example, you can write this:

pd.player.game

So this leads to :

var myNewObject = {
   game: pd.player.game,
   plat: pd.player.plat,
   name: pd.player.name,
   tag: pd.player.tag,
   score: pd.player.score,
   timePlayed: pd.player.timePlayed,
 }

Then, you have to transform this JS object literal into a JSON by using the JSON.stringify() method:

console.log(JSON.stringify(myNewObject, null, '\t'));

It returns what you want:

    {
        "game": "bf4",
        "plat": "pc",
        "name": "m4jes",
        "tag": "BPt",
        "score": 16724643,
        "timePlayed": 1476950
    }

So here is the full code:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
var jqxhr = $.get( "https://api.bf4stats.com/api/playerInfo?plat=pc&name=m4jes&output=js", function() {
  pd = eval(jqxhr.responseText);

  var myNewObject = {
   game: pd.player.game,
   plat: pd.player.plat,
   name: pd.player.name,
   tag: pd.player.tag,
   score: pd.player.score,
   timePlayed: pd.player.timePlayed,
 }

 console.log(JSON.stringify(myNewObject, null, '\t'));
});
</script>
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • thanks @hugues, but when i console it 'console.log(body.pd.player.game);' i got the error message as " Cannot read property 'player' of undefined " why it is so? – Sulu.MeanStack Aug 14 '17 at 08:21
  • Why are you using body? Try "console.log(pd.player.game)" –  Aug 14 '17 at 08:32
  • "pd is not defined" is the error msg after trying the above code "console.log(pd.player.game)". – Sulu.MeanStack Aug 14 '17 at 09:21
  • pd is the objects that is returned by the url. Can you sens me this url? –  Aug 14 '17 at 12:18
  • I added the procedure to get the string from the url. It should work. –  Aug 14 '17 at 13:18