0

$("#ELEMENT").TEXT() is giving me and html string.

<div id="ELEMENT">
{
    "products": [
        {
            "TPNB": "52260983",
            "name": "name1",
            "price": 0.89,
            "quantity": 1
        },
        {
            "TPNB": "73559869",
            "name": "name2",
            "price": 1.1,
            "quantity": 1
        },
        {
            "TPNB": "51447540",
            "name": "Tesco Dijon Mustard 185g",
            "price": 0.55,
            "quantity": 1
        },
        {
            "TPNB": "61227739",
            "name": "name3150ml",
            "price": 0.6,
            "quantity": 1
        },
        {
            "TPNB": "56925638",
            "name": "Tesco Chicken Thighs 1kg",
            "price": 2.5,
            "quantity": 2
        }
    ]
}
</div>

I want to extract this Array into following index

  1. "TPNB":"52260983","name":"name1","price":0.89,"quantity":1
  2. "TPNB":"52260983","name":"name2","price":0.89,"quantity":1
  3. "TPNB":"56925638","name":"name13","price":2.5,"quantity":2
AdityaParab
  • 7,024
  • 3
  • 27
  • 40

4 Answers4

0
var parsedJson = JSON.parse($("#ELEMENT").text())
var finalArray = new Array()
for (var i in parsedJson.products)
{
    finalArray.push(parsedJson.products[i]);
}
for (var i in finalArray)
{
    console.log(JSON.stringify(finalArray[i]).replace("{", "").replace("}", ""))  
}

after this you can access properties as such

finalArray[0].TPNB
finalArray[0].name
finalArray[0].price
finalArray[0].quantity

for the full code open you browser console and check out this http://jsfiddle.net/5wd29qch/1/

Liviu Boboia
  • 1,734
  • 1
  • 10
  • 21
  • it is not a json object... i am featching values from a render html page.. $("#someelementid").text(); and i am getting a whole string – user1036103 Nov 03 '15 at 15:22
  • what string are you getting? – Liviu Boboia Nov 03 '15 at 15:24
  • Pls see my question , I have modified it slightly. Thanks – user1036103 Nov 04 '15 at 09:31
  • i edited my response if i understand correctly you want to extract the values from the products key into an array – Liviu Boboia Nov 04 '15 at 09:53
  • Thanks.. it seems to be working fine but there is slight change how i want to populate this. i want output similar to this.. we need to find sub string after comma. I want to print TPNB, NAME ,PRICE , QTY IN one string Output should be "TPNB":"52260983","name":"name1","price":0.89,"quantity":1 "TPNB":"52260983","name":"name2","price":0.89,"quantity":1 "TPNB":"56925638","name":"name13","price":2.5,"quantity":2 At present it is printing 50507235 Tesco Limes Each 0.3 13 1 – user1036103 Nov 04 '15 at 10:36
  • I updated the code and jsfiddle, let me know if this is the result you want – Liviu Boboia Nov 04 '15 at 11:06
0

If the text is a valid json, you can parse it and get the products property which is the array you want:

var raw = $("#ELEMENT").text(),
    parsed = JSON.parse(raw),
    products = parsed.products;

console.log(products);
console.log(products[0]);
console.log(products[1]);
console.log(products[2]);
taxicala
  • 21,408
  • 7
  • 37
  • 66
0
    var jsonData = // ur data from .text()
    var productData = JSON.parse(jsonData);
    var productRows = productData.products;
    for(var i = 0; i < productRows.length; i++)
    {
          alert(productRows[i]);
    }
Developer
  • 759
  • 2
  • 9
  • 24
  • Try to add a full answer, not only the code, and also use an array to populate it with values, not an alert – micnic Nov 04 '15 at 10:07
0

Try this:

var json = $.parseJSON($('#ELEMENT').text());

json.products.map(function(v, i){
    console.log(JSON.stringify(v).replace('{','').replace('}',''));
});

Output:

"TPNB":"52260983","name":"name1","price":0.89,"quantity":1
"TPNB":"73559869","name":"name2","price":1.1,"quantity":1
"TPNB":"51447540","name":"Tesco Dijon Mustard 185g","price":0.55,"quantity":1
"TPNB":"61227739","name":"name3150ml","price":0.6,"quantity":1
"TPNB":"56925638","name":"Tesco Chicken Thighs 1kg","price":2.5,"quantity":2
SSS
  • 92
  • 4
  • Brilliant - thanks, One question, can we store new values in a variable. – user1036103 Nov 04 '15 at 14:39
  • Try this and let me know - [Working Demo](https://fiddle.jshell.net/mrbjc4Lr/). Here, all the values will be stored in an array called 'newArray'. You can access any value by just calling newArray[0] or newArray[1] or newArray[2] etc. – SSS Nov 05 '15 at 07:48