使用Javascript和PHP读取JSon字符串
Read JSon string with Javascript and PHP
在Javascript文件中,我将这个JSon字符串接收到一个隐藏的html字符串中:
<input id="page_json_language_index" type="hidden" value="[{"id":"label_accept_terms","fr":"En cliquant sur le bouton ci-dessous, vous accepter de respecter les "},{"id":"label_and","fr":" et la "},{"id":"label_birthdate","fr":"Anniversaire"},{"id":"label_bottom_about","fr":"'u00c0 propos de GayUrban"},{"id":"label_bottom_contact","fr":"Contactez-nous"},{"id":"label_bottom_copyright","fr":"'u00a9 2010-2013 GayUrban.Com - Tous droits r'u00e9serv'u00e9s"},{"id":"label_bottom_privacypolicy","fr":"Vie priv'u00e9e"},{"id":"label_bottom_termsofuse","fr":"Conditions d`...mon courriel"},{"id":"label_signon_twitter","fr":"Avec Twitter"},{"id":"label_slogan","fr":"LE site des rencontres LGBT !"},{"id":"label_terms_of_use","fr":"Conditions d`utilisation"},{"id":"label_title","fr":"Bienvenue sur GayUrban | LE site des rencontres LGBT !"},{"id":"label_transgender","fr":"Transgendre"},{"id":"label_username","fr":"Nom d`utilisateur"},{"id":"label_wait_create_profile","fr":"Un moment SVP, Cr'u00e9ation de votre profil en cours..."},{"id":"label_your_gender","fr":"Votre 'u00eate"}]">
从用户语言的MySQL数据库(这个例子是法语(fr),所以我需要在Javascript中访问这个"id"的每个"id"和每个值
示例:对于第一个"id"
我需要获得每个ID和VALUE 的单独变量
var label="label_accept_terms";和其他变量var value="在特定情况下,您接受相应的"
所以我在阅读时遇到了一个问题,并影响了每个具有良好标签和价值的ID。
谢谢你的帮助!
我必须指出,您在HTML中犯了一个错误。您应该转义引号以避免破坏属性,例如,只需将它们替换为撇号:
<input id="page_json_language_index" type="hidden" value='[{"id":"label_accept_terms","fr":"En cliquant sur le bouton ci-dessous, vous accepter de respecter les "},{"id":"label_and","fr":" et la "},{"id":"label_birthdate","fr":"Anniversaire"},{"id":"label_bottom_about","fr":"'u00c0 propos de GayUrban"},{"id":"label_bottom_contact","fr":"Contactez-nous"},{"id":"label_bottom_copyright","fr":"'u00a9 2010-2013 GayUrban.Com - Tous droits r'u00e9serv'u00e9s"},{"id":"label_bottom_privacypolicy","fr":"Vie priv'u00e9e"},{"id":"label_bottom_termsofuse","fr":"Conditions d`...mon courriel"},{"id":"label_signon_twitter","fr":"Avec Twitter"},{"id":"label_slogan","fr":"LE site des rencontres LGBT !"},{"id":"label_terms_of_use","fr":"Conditions d`utilisation"},{"id":"label_title","fr":"Bienvenue sur GayUrban | LE site des rencontres LGBT !"},{"id":"label_transgender","fr":"Transgendre"},{"id":"label_username","fr":"Nom d`utilisateur"},{"id":"label_wait_create_profile","fr":"Un moment SVP, Cr'u00e9ation de votre profil en cours..."},{"id":"label_your_gender","fr":"Votre 'u00eate"}]'>
JSON.parse是转换JSON字符串的最佳方式,但它不受旧IE(例如IE6)的支持。您可以使用JSON2使其兼容,也可以简单地使用eval()。
意识到滥用eval()可能导致XSS(跨站点脚本)漏洞。确保要解析的JSON是安全的(不包括恶意Javascript)。
下面是一个读取所有id:的示例
<input id="page_json_language_index" type="hidden" value='[{"id":"label_accept_terms","fr":"En cliquant sur le bouton ci-dessous, vous accepter de respecter les "},{"id":"label_and","fr":" et la "},{"id":"label_birthdate","fr":"Anniversaire"},{"id":"label_bottom_about","fr":"'u00c0 propos de GayUrban"},{"id":"label_bottom_contact","fr":"Contactez-nous"},{"id":"label_bottom_copyright","fr":"'u00a9 2010-2013 GayUrban.Com - Tous droits r'u00e9serv'u00e9s"},{"id":"label_bottom_privacypolicy","fr":"Vie priv'u00e9e"},{"id":"label_bottom_termsofuse","fr":"Conditions d`...mon courriel"},{"id":"label_signon_twitter","fr":"Avec Twitter"},{"id":"label_slogan","fr":"LE site des rencontres LGBT !"},{"id":"label_terms_of_use","fr":"Conditions d`utilisation"},{"id":"label_title","fr":"Bienvenue sur GayUrban | LE site des rencontres LGBT !"},{"id":"label_transgender","fr":"Transgendre"},{"id":"label_username","fr":"Nom d`utilisateur"},{"id":"label_wait_create_profile","fr":"Un moment SVP, Cr'u00e9ation de votre profil en cours..."},{"id":"label_your_gender","fr":"Votre 'u00eate"}]'>
<textarea id="debug-console" cols="50" rows="20"></textarea>
<script type="text/javascript">
var arr = eval(document.getElementById("page_json_language_index").value),
output = document.getElementById("debug-console");
//output all id
for(var i=0; i<arr.length; i++)
output.value += [i, ": ", arr[i].id, "'n"].join("");
//show the first id
alert(arr[0].id);
</script>
实际上,您可以直接将JSON输出到Javascript。为了满足你的需求,我想这就是你需要的。
<?php
...
$data = ...; //for example, from mysql query results
$language = "fr"; //you can replace it with it/en/zh...
...
?>
<input id="some_id"></input>
<script>
(function() {
var i18n = "<?php echo json_encode($data); ?>",
lang = "<?php echo $language; ?>", //language
data = eval(i18n); //you can also use JSON.parse/jQuery.parseJSON ...
for(var i=0; i<data.length; i++) {
document.getElementById(data[i].id).value = data[i][lang]; //a general way to read object's attribute in Javascript
}
})()
</script>
我需要获得每个ID和VALUE 的单独变量
这不是办法,你不想用一堆变量污染你的范围。您所拥有的是一个集合(对象数组)。您可以循环这样的集合并访问所需的属性。
var input = document.getElementById('page_json_language_index');
var data = JSON.parse(input.value); // collection
目标是将(id,fr)值分配给jquery中的每一对标签
var label = function(lab) {
return '<label id="'+ lab.id +'">'+ lab.fr +'</label>';
};
var labels = data.map(label);
您也可以将其作为jQuery集合:
var $labels = $(labels.join(''));
然后你可以将它附加到任何容器:
$labels.appendTo('body');
相关文章:
- 如何使用JSON字符串中的jQuery填充下拉框
- Json字符串可以'当字符串末尾有“'时,t解码;
- jquery从2个json字符串构建一个复选框表单
- RegEx只匹配JSON字符串中最里面的数组
- json字符串的Javascript帖子:没有任何东西传入或返回
- 使用javascript在MVC视图中解析.net JSON字符串
- 用Angularjs+Typescript将GZip Json字符串解压为普通Json字符串
- 如何将表单数据传递给PHP并将json字符串返回给Javascript
- 如何使用AngularJS应用基于Json字符串响应的条件CSS类
- 停止JSON.parse()从JSON字符串数据中删除尾随零
- 将JSON字符串转换为函数参数
- 在AngularJS中创建JSON字符串
- 为什么我不能使用jQuery.parseJSON(json)解析json字符串
- Json字符串更改日期
- JSON字符串格式错误
- Json和$scope的角度之间的差异$eval应用于JSON字符串时
- C#JSON字符串到JavaScript数组
- 如何在JSON字符串中实现嵌套HTML
- 如何从带有多个对象的JSON字符串创建JSON对象
- 使用捕获组查找和替换json字符串中出现的所有短语