1

I am new to javascript, sorry for silly question and possible duplicate. Please suggest me efficient way of parsing json. I would like to fetch list of strings Maktg:

{  
   "d":{  
      "results":[  
         {  
            "Maktg":"BATTERY",
            "W":"1000",
            "IS":"",
            "IM":"",
            "IW":"",
            "__metadata":{  
               "type":"s",
               "uri":"https://some_url)"
            },
            "IMaktg":"",
            "Matnr":"0001",
            "Stlan":"1"
         },
         {  
            "Maktg":"CONTROL",
//etc...

3 Answers 3

1

We have a JSON:

{  
   "d":{  
      "results":[  
         {  
            "Maktg":"BATTERY",
            "W":"1000",
            "IS":"",
            "IM":"",
            "IW":"",
            "__metadata":{  
               "type":"s",
               "uri":"https://some_url"
            },
            "IMaktg":"",
            "Matnr":"0001",
            "Stlan":"1"
         }
      ]
   }
}

Lest convert string JSON into more useful JavaScript object:

The JSON.parse() method parses a string as JSON, optionally transforming the value produced by parsing.

var 
  jsonStr = '{"d":{"results":[{"Maktg":"BATTERY","W":"1000","IS":"","IM":"","IW":"","__metadata":{"type":"s","uri":"https://some_url"},"IMaktg":"","Matnr":"0001","Stlan":"1"}]}}';
  jsonObj = JSON.parse(jsonStr),
  results = jsonObj.d.results;

for (var i in results) {

  console.log(results[i]['Maktg']);

  /*
    results[i]['W']
    results[i]['IS']
    results[i]['IM']
    results[i]['__metadata']['type']
    and etc...

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

Comments

1

Try this:

var jsonArray = yourJSON.d.results;
var results = [];
jsonArray.forEach(function(object){
    results.push(object.Maktg);
}
console.log(results);

Comments

0

Please try to get output with JSON.parse like this.

var getData = JSON.parse(data);

for(i=0;i<getData.d["results"].length;i++)
{
   alert(getData.d["results"][i].Maktg);
   alert(getData.d["results"][i].W);
   //etc...
}

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.