I have the following JSON value pushed from the server.
result=[{"id":1492,"name":"Delhi"},
{"id":109,"name":"Coimbatore"},
{"id":576,"name":"Konni"},
{"id":525,"name":"Kottayam"}
]
I know how to convert JSON Array to Javascript Array.Here is the code below(got from stackoverflow)
var locations = [];
$.each(result, function(i, obj) {
locations.push([obj.id,obj.name]);
});
I want to convert this JSON Array to a JavaScript Object Array so that I can access the values as jarray[0].id which will give me the value 1492. Please advice
You don't need to do anything to the result array. Just use result[0].id and it will evaluate to 1492.
Related
i want to convert a json array (string) to javascript array using just some specific values. The json array is :
[{"id":47,"libelle":"famille de test"},{"id":1,"libelle":"GEOLOCALISATION"},{"id":4,"libelle":"OUTILS"},{"id":2,"libelle":"PROPRETE"},{"id":3,"libelle":"URGENCE"}]
and i want to get something like this ["famille de test", "GEOLOCALISATION", ...] using just libelle values.
I tried to use $.map but didn't work out.
The map implementation should work:
var jsonStr = '[{"id":47,"libelle":"famille de test"},{"id":1,"libelle":"GEOLOCALISATION"},{"id":4,"libelle":"OUTILS"},{"id":2,"libelle":"PROPRETE"},{"id":3,"libelle":"URGENCE"}]';
var arr = JSON.parse(jsonStr);
var libelle = arr.map(function(x) { return x.libelle; });
First, you must turn your JSON string into a JavaScript Array by using JSON.parse(yourJSONString). After that, it is a simple JavaScript array and you can use the map method you tried
I am using ajax to get the data from the database. After getting data from table I am doing json_encode to get the data in json format. After that I am doing parseJSON to show the data in js.
when I am getting the data in json I just did
data = $.parseJSON(data);
console.log(data);
I got the data like this jQuery object.
From here I want to get the values of firstname.
I tried console.log(data.first_name);
but it not worked. It is showing undefined in the console tab. So can someone tell me how to get the first_name value here
Your data is array of objects and has data on indexes 0,1,2 so on so you need
try
console.log(data[0].first_name);
you can also loop through them
for(var a=0;a<data.length;a++) {
console.log(data[a].first_name);
}
You have been returned an array of objects. Iterate through all of them using:
for(var i = 0; i < data.length; i++) {
console.log(data[i].first_name);
}
jquery provide $.each() function to iterate object or array, see below sample code
data = $.parseJSON(data);
$.each(data, function(index, object){
console.log(object.first_name);
})
It looks like data is array of objects you have to iterate over this array and get the first_name attribute of each object in it as follows,
data = $.parseJSON(data);
data.forEach(function(item){
console.log(item.first_name);
})
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
I have a JavaScript variable with comma separated string values - i.e. value1,value2,value3, ......,valueX,
I need to convert this variable's values into a JSON object. I will then use this object to match user enteredText value by using filterObj.hasOwnProperty(search)
Please help me to sort this out.
What you seem to want is to build, from your string, a JavaScript object that would act as a map so that you can efficiently test what values are inside.
You can do it like this :
var str = 'value1,value2,value3,valueX';
var map = {};
var tokens = str.split(',');
for (var i=tokens.length; i--;) map[tokens[i]]=true;
Then you can test if a value is present like this :
if (map[someWord]) {
// yes it's present
}
Why JSON? You can convert it into an array with split(",").
var csv = 'value1,value2,value3';
var array = csv.split(",");
console.log(array); // ["value1", "value2", "value3"]
Accessing it with array[i] should do the job.
for (var i = 0; i < array.length; i++) {
// do anything you want with array[i]
}
JSON is used for data interchanging. Unless you would like to communicate with other languages or pass some data along, there is no need for JSON when you are processing with JavaScript on a single page.
JavaScript has JSON.stringify() method to convert an object into JSON string and similarly JSON.parse() to convert it back. Read more about it
All about JSON : Why & How
Cheers!!
JSON format requires (single or multi-dimensional) list of key, value pairs. You cannot just convert a comma separated list in to JSON format. You need keys to assign.
Example,
[
{"key":"value1"},
{"key":"value2"},
{"key":"value3"},
...
{"key":"valueX"}
]
I think for your requirement, you can use Array.
My code returns a JSON array, I think. The returned JSON array is stored in a javascript variable: result. If I
console.log(result);
in FF, I get the output
[{"id":"G24","value":"Zas, S"},{"id":"G75","value":"Wara, TS"},{"id":"G48","value":"Jala, S"}]
Validated on jsonLint to be correct json.
In my code, if I count the number of elements in the array like so:
var key, count = 0;
for(key in result)
{
count++;
}
console.log("result count is:" + count);
The output is 94 which is the length/count of characters in the array - [the sample output shown above is modified]
However, in the JSON tab in FF, it shows the result as being an array of objects:
0 Object { id="G24", value="Zas, S"}
1 Object { id="G75", value="Wara, TS"}
2 Object { id="G48", value="Jala, S"}
I have used alternative code pieces from 'stackoverflow' sources
for ( property in result )
{
if(result.hasOwnProperty(property))
{
count++;
}
}
And this has had the same outcome. How can I have iterate rightly over this array or array of objects or string or whatever else it is? And get the count please?. Thanks.
It sounds like you have an HTTP response returning a JSON document.
In the JSON tab, this is shown as JSON.
If your code, you are just taking the text of the response and operating on that.
You need to parse the JSON to create JavaScript objects.
Pass the string through JSON.parse and use json2.js to polyfill for older browsers.
You have to parse the JSON to create a JavaScript Array.
result = JSON.parse(result); // Now it's an array
console.log(result.length) // prints now what you want