Show Json Object In TextBox - javascript

I have json returned from Database.I want to pick only one object Value and show it in the textbox. Here is my json.
[{
"ErrorMessage":"",
"ID":294,
"ExpenseID":0,
"EffectiveDate":"/Date(1262284200000)/",
"FormattedEffectiveDate":"01-01-2010",
"Perunit":null,
"VATRate":17.5,
"ChangedByID":1,
"ChangedByName":"superuser, superuser",
"Expense":null,
"ErrorSummary":null,
"ErrorList":[]
}]
I have Tried
var Jsoninvoice = JSON.stringify(data)
alert(Jsoninvoice.VATRate) and also alert(data.VATRate)
Thank you In advance.

You have an array containing 1 object. stringify turns this object into a string - you need it parsed so you can use it.
(I'm not sure if the object is parsed already, so to cover all bases, we'll parse it)
var Jsoninvoice = JSON.parse(data);
alert(Jsoninvoice[0].VATRate);
You have to specify the arrays index before you can access the properties.

It is already json object and stringify is not needed as #tymJV said you need to parse it if it is returned as string, just you need to access array item, as it is an array:
alert(data[0].VATRate)
SEE FIDDLE

You could use $.parseJSON(YOURJSON), and then use the keys to pull the data. Since it's in an array, you'll have to use [0] to pull the first item in the array (ie: your data).
Example
$(document).ready(function(){
var j ='[{"ErrorMessage":"","ID":294,"ExpenseID":0,"EffectiveDate":"/Date(1262284200000)/","FormattedEffectiveDate":"01-01-2010","Perunit":null,"VATRate":17.5,"ChangedByID":1,"ChangedByName":"superuser, superuser","Expense":null,"ErrorSummary":null,"ErrorList":[]}]';
var json = $.parseJSON(j);
alert("VATRate: "+json[0].VATRate);
});
Fiddle for reference

Related

How do I access the value of an array property?

How do I access the value stored in the description property using javascript?
As seen in the image below (a screenshot from the browser console), the stored value is [STK_CB - ] Request Cancelled by user
In the browser console, I have tried console.log(responseMan.payload["0"].jsonPayload.description); which shows undefined. Where am I going wrong?
Looking forward to your help.
The value of jsonPayload is a string, not an object -- notice the double quotes around it. And the name of the property implies that it's JSON. You need to call JSON.parse() to convert it to an object.
var payload = JSON.parse(responseMan.payload[0].jsonPayload);
console.log(payload.description);
just remove " from index and do it like:
u also have to cast the json first
const jsonStr = responseMan.payload[0].jsonPayload;
const data = JSON.parse(jsonStr);
console.log(data.description);
The problem is in your json. Because you are trying to access responseMan.payload ["0"]. JsonPayload up there is a correct element in the json. But the description is a string value in responseMan.payload ["0"]. JsonPayload, therefore, must format the contents of responseMan.payload ["0"]. JsonPayload on a json object. Example var obj = JSON.parse (responseMan.payload ["0"]. JsonPayload); and so
description will be a json object
OR
You can fix that json in your backend and send that section as a json and not as a string

Can't access an element in JSON array

No one really answered this question but how on earth can one use this JSON return data from a php/mysql direct using JavaScript?
Here is the return data once i used JSON.parse and saved it to the Javascript variable obj
[{"stuid":"10-00002","stuname":"Meratis, Velino","stucourse":"Arts","stustat":"0","stulyear":"4","stulog":"feb 16 2017"},{"stuid":"10-00003","stuname":"Melker, Alana","stucourse":"Wilderness","stustat":"1","stulyear":"5","stulog":"feb 16 2017"}]
I've tried the simple obj.stuname but it returns only an undefined i've tried many times to understand it but i can't seem to use this array at all.
Could anyone help on this?
I've also tried the reObj = {"stu":obj}; style but then it only returns an [object Object]
so please someone elaborate on this?
obj is a json array, so you have to access an element using its index.
Also, you have to use JSON.parse in order to turn a string of json text to a Javascript object.
Try this:
var stuname=obj[0].stuname;
var obj='[{"stuid":"10-00002","stuname":"Meratis, Velino","stucourse":"Arts","stustat":"0","stulyear":"4","stulog":"feb 16 2017"},{"stuid":"10-00003","stuname":"Melker, Alana","stucourse":"Wilderness","stustat":"1","stulyear":"5","stulog":"feb 16 2017"}]';
var objParsed=JSON.parse(obj);
console.log(objParsed[0].stuname);
If you want to iterate array, use forEach method.
var obj='[{"stuid":"10-00002","stuname":"Meratis, Velino","stucourse":"Arts","stustat":"0","stulyear":"4","stulog":"feb 16 2017"},{"stuid":"10-00003","stuname":"Melker, Alana","stucourse":"Wilderness","stustat":"1","stulyear":"5","stulog":"feb 16 2017"}]';
var objParsed=JSON.parse(obj);
objParsed.forEach(function(item){
console.log(item.stuname);
});
If you are getting this response from php via ajax.
Be sure to use dataType as json to get json type response not string.
Otherwise you need to parse json data like this
obj = JSON.parse(jsonStrFromPhp);
Then you can fetch data as obj.stuname or obj[0].stuname depends how you returned from php like this {"stu":obj} or like this [{"stu":obj}]
while i was having a migraine... of why my code was wrong it turned out that even though the obj = JSON.parse(jsonStrFromPhp); returned only an [object Object],[object Object] javascript can actually understand that shins and returned my variable... how confusing.

Parse the JSON Object with Index or position instead of keyname using Jquery?

My Existing Code and Explanation
Working Fine, But I want to Create a template like Its should read JSON Data that Having keys in any name, I am Always reading data from JSON object in 2nd Position
Now I read the JSON that passed in the argument (data) inside that using the for loop extract single object in group, after that get the data using key(Module_Name).. I know the key(Module_Name) is always present in second position only, Now its Working Fine with keyname rather i want to fetch the data using position, that also used as template in my other modules
/* render Screen Objects */
screenRenderer.displayScreens = function(data)
{
screenRenderer.renderLayout(function(cont, boo)
{
if (boo)
{
for(i=0;i<data.length;i++)
{
var buttons = "<button class='screen_button'>"+data[i].Module_Name+"</button>";
$("#screen-cont").append(buttons);
}
}
else
{
scr_cont.show();
}
})
}
This is My Requirement, kindly awaiting suggestions, answers..
Thanks in Advance
You can use the function Object.keys(obj) to get all keys of an object as array, which you can access with an Index and than use the String you get to access the property.
So it could look like this:
var obj = data[i];
var keys = Object.keys(obj);
var result = obj[keys[1]];
Hope this is what you want.

Can anyone please tell how to iterate and use a json array of maps in jsp?

I have my json data (array of map) in my javascript and it's like as below:
[
{"HOURLY":"$309.75","Airport Fee":"$15.00","STC":"$52.66","Gratuity":"$61.95","Fuel Surcharge":"$5.00"},
{"HOURLY":"$309.75","Airport Fee":"$15.00","STC":"$52.66","Gratuity":"$61.95","Fuel Surcharge":"$5.00"}
]
I want to use the values of one of the maps in my jsp using some object.value or map.getValueBykey mechanism as if just like we use a model object passed from java to jsp.
Seeing that you have an array of objects, treat it first like an array. So,
prices = [
{"HOURLY":"$309.75","Airport Fee":"$15.00","STC":"$52.66","Gratuity":"$61.95","Fuel Surcharge":"$5.00"},
{"HOURLY":"$329.75","Airport Fee":"$15.00","STC":"$52.66","Gratuity":"$61.95","Fuel Surcharge":"$5.00"}
];
console.log(prices[0].HOURLY); // will output "$309.75", value found in the first object
console.log(prices[1].HOURLY); // will output "$329.75", value found in the second object
// If you want to get all values of that one property in all objects in the array, you can iterate like this:
for(var i =0;i <prices.length;i++){
console.log( prices[i].HOURLY);
}
// If you want to get all values for each property in a specific object, for example the first one in the array, you can iterate like this :
for(key in prices[0]){
console.log("'" +key + "' = " + prices[0][key]);
}
This is how it's done in JavaScript.
I see you are asking about JSP however. Maybe an example like this is what you are looking for:
javax.servlet.ServletException: javax.servlet.jsp.JspTagException: Don't know how to iterate over supplied "items" in <forEach>

Retrieve JSON Array element value

My web service returned a JSON Array (ie. [{"key":"value"}, {"key":"value2"}]). In the array there are two items as you can see, which are separated with comma. I want to know how can I access the second item, and get the value of "key" for the second item.
I've tried:
var a = msg.d[1].key
With no success of course.
This is the returned string:
"[{"Code":"000000","Name":"Black","Id":9},{"Code":"BF2C2C","Name":"Red","Id":11}]"
The string was extracted using FireBug after watching the msg.d.
Need your help in solving this.
msg[1].key
Assuming that the name of that array is msg. I'm not sure what you are using .d for.
If msg.d is a string representing an array, use JSON.parse.
JSON.parse(msg.d)[1].key
You can replace key with the key you are wanting, e.g. Code, Name, Id, etc.
This works as expected for me.
var msg = [{"key":"value"}, {"key":"value2"}];
var a = msg[1].key;
What is msg in the example above? Need more info to help.
If msg.d is a string then you have to eval (uggh) or parse it before applying the array subscript.

Categories