ViKu February 2016

Converting json object to a string

I'm generating an json object as follows, i need to iterate and fetch the value inside it in javascript.

[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

and if i stringify it, I'll get following values,

[[{"K":"43800001","D":"Resident Demand Deposit"},{"K":"43800099","D":"Not Applicable"},
{"K":"43800014","D":"Cash"},
{"K":"43800012","D":"Income and Expenditure"},
{"K":"43800011","D":"Other Liabilities"},
{"K":"43800010","D":"Other Assets"},
{"K":"43800009","D":"Bankers"},
{"K":"43800008","D":"Bar"},
{"K":"43800007","D":"DD"},
{"K":"43800006","D":"HO"},
{"K":"43800005","D":"Advance"},
{"K":"43800004","D":"Investments"},
{"K":"43800003","D":"Bills"},
{"K":"43800002","D":"Resident Term Deposit"}]]

There are 14 record pairs it contains as K holding the code, and the D holding the description of code. i need to iterate each K, D pair data.

I'm trying as follows, reusult is holding the 14 [object object] value as show above.

result[0].K.toString();
result[0].D.tostring();

I'm getting type error.

Answers


Vikash Pandey February 2016

I would recomend to use stringify.

Example:

JSON.stringify({ x: 5 })

Here in your case you can store your entire object or array in a variable and pass it to stringify method:

Example:

var data = { x: 5 };

JSON.stringify(data) 

output will be like:

'{"x":5}'

Hoping this will help you :)


Rayon Dabre February 2016

Use JSON.stringify(obj), this method converts a JavaScript value to a JSON string.

Edit:

You do not need to convert values to string as they are of type string.

Try this:

var obj = [{
  "K": "43800001",
  "D": "Resident Demand Deposit"
}, {
  "K": "43800099",
  "D": "Not Applicable"
}, {
  "K": "43800014",
  "D": "Cash"
}, {
  "K": "43800012",
  "D": "Income and Expenditure"
}, {
  "K": "43800011",
  "D": "Other Liabilities"
}, {
  "K": "43800010",
  "D": "Other Assets"
}, {
  "K": "43800009",
  "D": "Bankers"
}, {
  "K": "43800008",
  "D": "Bar"
}, {
  "K": "43800007",
  "D": "DD"
}, {
  "K": "43800006",
  "D": "HO"
}, {
  "K": "43800005",
  "D": "Advance"
}, {
  "K": "43800004",
  "D": "Investments"
}, {
  "K": "43800003",
  "D": "Bills"
}, {
  "K": "43800002",
  "D": "Resident Term Deposit"
}];
var html = '';
obj.forEach(function(item) {
  html += '<div>Code: ' + item.K + ' Description: ' + item.D + '</div>'
});
document.body.innerHTML = html;


hraw23 February 2016

try this:

$.each(obj, function(i, item) {
   var data_k = item.K;
   var data_d = item.D;
});


Azim February 2016

You can iterate through each pair of K & D like following.

var result = [[
    { "K": "43800001", "D": "Resident Demand Deposit" },
    { "K": "43800099", "D": "Not Applicable" },
    { "K": "43800014", "D": "Cash" }
]];

$.each(result[0], function () {
    console.log(this.K);
    console.log(this.D);
})

Post Status

Asked in February 2016
Viewed 1,857 times
Voted 10
Answered 4 times

Search




Leave an answer