{"TeamList" : [{"teamid" : "2","teamname" : "Milan"}]}
How do i write the code to read the teamid and teamname so as to store them in seperate variables?
Please Help!
If it is a JSON string, parse it...
var obj = jQuery.parseJSON(jsonString);
Then work with the information
obj.TeamList[0].teamid;
obj.TeamList[0].teamname;
TeamList is an array so if you have more than one "team" you'll need to loop over them.
You have an object containing an array TeamList, which has one object as its elements:
var tl = {"TeamList" : [{"teamid" : "2","teamname" : "Milan"}]};
var id = tl.TeamList[0].teamid;
var name = tl.TeamList[0].teamname;
If the example you have posted in contained as a string you can parse it like so with javascript...
var jsonObject = JSON.parse(myJsonString);
you can then access your array like so...
jsonObject.TeamList
and each item in TeamList...
jsonObject.TeamList[i].teamid
jsonObject.TeamList[i].teamname
finally assuming you have one item in TeamList and making an attemp to directly answers you question...
var teamid = jsonObject.TeamList[0].teamid;
var teamname = jsonObject.TeamList[0].teamname;
hope that makes sense
in which language? Basically after parsing using json you would do something like this on the result:
result["TeamList"][0]["teamname"] to get teamname and result["TeamList"][0]["teamid"] to get teamid.
If you can use json_decode, like this :
$content = '{"TeamList" : [{"teamid" : "2","teamname" : "Milan"}]}';
$json = json_decode($content);
$obj = $json->{'TeamList'}[0];
print $obj->{'teamid'}."//".$obj->{'teamname'};
You had tagged your question as jQuery? We're you wanting to display this information on a page?
Given some sample html:
<label>Team ID:</label>
<div id="teamid"></div>
<label>Team Name:</label>
<div id="teamname"></div>
And a little jquery:
var obj = {"TeamList" : [{"teamid" : "2","teamname" : "Milan"}]};
$('#teamid').html(obj.TeamList[0].teamid);
$('#teamname').html(obj.TeamList[0].teamname);
Would allow you to accomplish this. As others have pointed out you would need to iterate over the collection if there were multiple teams.
Related
I am not able to parse the jsonArray in the viewBag. I want to get the contents and then use them add options to a
Upon using a console.log I could see that my json is as follows :
So far I have an array with a single element
[
{
"BackgroundColor":null,
"BaseFormat":"PNG",
"ColorFormat":"RGB",
"ColorProfile":null,
"Density":"300",
"Extra_param":null,
"FileFormat":"JPG",
"PresetId":"2",
"Resolution":"",
"Quality":100
}
]
I have the contents in a variable as below:
var presetArray = #Html.Raw(Json.Encode(#ViewBag.user_doc_presets));
console.log(presetArray);
I want to get the BaseFormat and colorformat to be used into a select.
I tried some stackoverflow posts but they didn't help me.
If you have a link to an old post or a hint please do share.
#downvoters, Please let me know if you have any questions regarding this post instead of downvoting covertly.
Since it is of array type you can access it with its Index and here it is 0 as the array element is only one.
presetArray[0].BaseFormat
presetArray[0].ColorFormat
var presetArray = [{"BackgroundColor":null,"BaseFormat":"PNG","ColorFormat":"RGB","ColorProfile":null,"Density":"300","Extra_param":null,"FileFormat":"JPG","PresetId":"2","Resolution":"","Quality":100}]
console.log("Base Format: " + presetArray[0].BaseFormat);
console.log("Color Format: " + presetArray[0].ColorFormat);
I was missing a Json.Parse, which solved the problem
Please refer to
http://jsfiddle.net/jresdqw3/
var arr = [
{
"BackgroundColor":null,
"BaseFormat":"PNG",
"ColorFormat":"RGB",
"ColorProfile":null,
"Density":"300",
"Extra_param":null,
"FileFormat":"JPG",
"PresetId":"2",
"Resolution":"",
"Quality":100
}
];
alert(arr.length);
alert(JSON.stringify(arr).length);
This is my html ( in twig template )
<li id="{{folder.id}}" data-jstree='{"icon":"glyphicon glyphicon-tags", "type":"folder"}' >{{folder.name}}
I am trying to get the value of 'type' from 'data-jstree'.
I tried using
var node_id = ref.get_node(sel[i]).id;
var type = $("#"+node_id).attr("data-jstree");
but that gives me this : {"icon":"glyphicon glyphicon-tag", "type":"tag"}
and i only need the value of type.
Thanks in advance.
var type = JSON.parse($("#"+node_id).attr("data-jstree")).type
you need to parse the string into json. do something like this:
var node_id = ref.get_node(sel[i]).id;
var type = $("#"+node_id).attr("data-jstree");
type = JSON.parse(type).type;
so I received the following array from another student and my job is to display it in the HTML.
I've followed examples found on here as well as other sites. It's my first attempt at doing this.
Anyway, I've called in the required values, but they're not displaying. I'd appreciate any help I can get.
This is the script block and I'll attach some of the HTML. Currently the json is situated before the closing head tag.
<script>
var height = {"height" : "1.76m"};
var weight = {"weight" : "65kg"};
var bmi = {weight/(height*height)};
var cholesterol = {"cholesterol" : "26mmol/d"};
var glucose ={"glucose" : "100mg/dl"};
var pressure = {"pressure" : "120/80"};
var pulseRate = {"pulse rate" : "80bpm"};
window.onload = function() {
obj = JSON.parse(height, weight, bmi, cholesterol, glucose, pressure, pulseRate);
document.getElementById("hgt").innerHTML = obj.height[1];
document.getElementById("wgt").innerHTML = obj.weight[1];
document.getElementById("bmi").innerHTML = obj.bmi[1];
document.getElementById("chol").innerHTML = obj.cholesterol[1];
document.getElementById("gluc").innerHTML = obj.glucose[1];
document.getElementById("bp").innerHTML = obj.pressure[1];
document.getElementById("rpr").innerHTML = obj.pulseRate[1];
};
</script>
<div class="col-xs-2">
<h3 class="heading">Today</h3>
<img class="picture2" src="images/icon-height.png" />
<div class="caption">
<h2 id="hgt"></h2>
<p>height</p>
</div>
Since your variables height, weight... do not have to be parsed, since they are valid JSON Objects, you could save yourself some time by assigning obj like this:
var obj = {
height: "1.76",
weight: "65kg",
...,
bmi: //your calculation here!
};
document.getElementById("yourid").innerHTML = obj.weight;
...
Since JSON objects use key:value pairs, you can simply access the values by using object.key or object['key'].
Since you're trying to access the values by using obj.weight[1] I'm guessing that you're trying to get the second value in { "weight" : "67kg" }. But since this is a valid Object, weight is the key and "67kg" is the value. So you are trying to get the value at index 1 on an Object (obj.weight[1]). For this to work your object must have a key 1 or be an array. I hope you get what I'm trying to explain to you.
To make it a lot more simpler, you could just use height.height instead of the not working obj.height[1], because your var height is an object with a key height. But for readability I would suggest the format from my snippet.
i'm trying to create object named client. and put it into a array. And after this, read the name of the 1st client.
Exemple :
client1 {nom : "marco", prenom : "chedel", adresseMail : "ssss#ggg.com"};
and put this client into an array like bellow.
listClients[client1]
what i did :
var listClients = [];
var client1 = {nom : "chedel",prenom:"Marco",adresseMail:"marco#gmail.com"};
listClients.push(client1);
var client2 = {nom : "De Almeida",prenom:"Jorge",adresseMail:"jorge#gmail.com"};
listClients.push(client2);
function afficheClients(tableau){
for (i=0;i<tableau.length;i++)
{
var c = tableau[i];
document.write(c[adresseMail]);
// This ^^ doesn't work, it says :adresseMail isn't definied in c
}
}
afficheClients(listClients);
You're treating adressMail as a variable and not as a string:
use
document.write(c["adresseMail"]);
There are two ways to access properties of an object:
obj.prop
obj['prop']
You are doing the following mixture which doesn't work: obj[prop].
Fix your code to c.adresseMail or c['adresseMail'] and it will work.
Either reference it using a string:
document.write(c['adresseMail']);
or using dot notation:
document.write(c.adresseMail);
And, yes - document.write should be avoided.
Consider i am having an object "req".
When i use console.log(req) i get
Object { term="s"}
My requirement is to append an value with the existing object.
My expected requirement is to be like this:
Object { term="s", st_id = "512"}
Is it possible to do the above?
If yes, how ?
Thanks in advance..
There are several ways to do it;
Plain javascript:
var req = { "term" : "s" };
req.st_id = "512";
Because javascript objects behaves like associative arrays* you can also use this:
var req = { "term" : "s" };
req["st_id"] = "512";
jQuery way $.extend():
var req = { "term" : "s" };
$.extend(req, { "st_id" : "512" });
You can do this in a couple of ways:
req.st_id = "512";
or
req["st_id"] = "512";
The second of these is especially useful if the variable name is dynamic, e.g. a variable could be used instead of the string literal:
var key = "st_id";
req[key] = "512";
Yes this is possible, to add properties to a value simply use the following syntax:
req.st_id = "512";