读取 Json 变量

发布于 2025-01-02 22:51:19 字数 145 浏览 0 评论 0原文

{"TeamList" : [{"teamid" : "2","teamname" : "Milan"}]}

我如何编写代码来读取 teamid 和 teamname 以便将它们存储在单独的变量中?

请帮忙!

{"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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

非要怀念 2025-01-09 22:51:19

如果它是一个 JSON 字符串,则解析它...

var obj = jQuery.parseJSON(jsonString);

然后处理信息

obj.TeamList[0].teamid;
obj.TeamList[0].teamname;

TeamList 是一个数组,因此如果您有多个“团队”,您需要循环遍历它们。

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.

作死小能手 2025-01-09 22:51:19

您有一个包含数组 TeamList 的对象,该数组有一个对象作为其元素:

var tl = {"TeamList" : [{"teamid" : "2","teamname" : "Milan"}]};

var id = tl.TeamList[0].teamid;
var name = tl.TeamList[0].teamname;

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;
夕嗳→ 2025-01-09 22:51:19

如果您发布的示例包含为字符串,您可以使用 javascript 像这样解析它...

var jsonObject = JSON.parse(myJsonString);

然后您可以像这样访问您的数组...

jsonObject.TeamList

以及 TeamList 中的每一项...

jsonObject.TeamList[i].teamid
jsonObject.TeamList[i].teamname

最后假设您在 TeamList 中有一项并且尝试直接回答你的问题......

var teamid = jsonObject.TeamList[0].teamid;
var teamname = jsonObject.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

野生奥特曼 2025-01-09 22:51:19

用哪种语言?基本上在使用 json 解析之后,您将对结果执行类似的操作:

result["TeamList"][0]["teamname"] 来获取团队名称和 result["TeamList"][ 0]["teamid"] 获取 teamid。

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.

夜雨飘雪 2025-01-09 22:51:19

如果您可以使用 json_decode,如下所示:

$content = '{"TeamList" : [{"teamid" : "2","teamname" : "Milan"}]}';
$json = json_decode($content);
$obj = $json->{'TeamList'}[0];
print $obj->{'teamid'}."//".$obj->{'teamname'};

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'};
情绪失控 2025-01-09 22:51:19

您已将您的问题标记为 jQuery?您想在页面上显示此信息吗?

给定一些示例 html:

<label>Team ID:</label>
<div id="teamid"></div>

<label>Team Name:</label>
<div id="teamname"></div>

和一点 jquery:

var obj = {"TeamList" : [{"teamid" : "2","teamname" : "Milan"}]};

$('#teamid').html(obj.TeamList[0].teamid);
$('#teamname').html(obj.TeamList[0].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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文