简体   繁体   English

如何更改JSON输出格式以及如何支持中文字符?

[英]How to change the JSON output format and how to support chinese character?

Currently I using the following code to get my JSON output from MySQL. 目前我使用以下代码从MySQL获取我的JSON输出。

<?php


$session = mysql_connect('localhost','name','pass'); 

mysql_select_db('dbname', $session); 


    $result= mysql_query('SELECT message FROM posts', $session); 
 $somethings = array();  
 while ($row = mysql_fetch_assoc($result)) {  
    $somethings[] = $row; 
 } 

?> 

<script type="text/javascript"> 
    var somethings= <?php echo json_encode($somethings); ?>; 
</script> 

And the output is: 输出是:

<script type="text/javascript"> 
    var somethings= [{"message":"Welcome to Yo~ :)"},{"message":"Try iPhone post!"},{"message":"????" (the ???? meant to be chinese character)}]; 
</script>

Here is the question, how can I change my output into format like : 这是一个问题,如何将输出更改为以下格式:

<script type="text/javascript"> 
userAge = new Array('21','36','20'),
userMid = new Array('liuple','anhu','jacksen');
</script>

Which I'll be using later with following code : 我稍后将使用以下代码:

 var html = '
<table class="map-overlay">
  <tr>
    <td class="user">' +
      '<a class="username" href="/' + **userMid[index]** + '" target="_blank"><img alt="" src="' +
        getAvatar(signImgList[index], '72x72') +
        '"></a><br>
      <a class="username" href="/' + **userMid[index]** + '" target="_blank">' +
      userNameList[index] +
      '</a><br>
      <span class="info">' + **userSex[index]** + ' ' + **userAge[index]** + '岁<br>
      ' +
      cityList[index] +
      '</span>' +
      '</td>
    <td class="content">' + picString
      + somethings[index] + '<br>
      <span class="time">' +
      timeList[index] + picTips +
      '</span></td>
  </tr>
</table>
'; 

Also how can my json output support chinese character? 另外我的json输出如何支持中文字符?

Thanks for helping and reading! 感谢您的帮助和阅读!

Best guessing your question: 最好猜测你的问题:

How to change this JSON format: 如何更改此JSON格式:

[{ key : "Value", key2 : "Value2" }, { key : "Another Value", key2 : "Another Value2" }]

To this format: 对于这种格式:

var Keys = ["Value", "Another Value"];
var Key2s = ["Value2", "Another Value2"];

The answer is: You shouldn't. 答案是:你不应该。 JSON is a good data format to work with, and it's self-contained. JSON是一种很好的数据格式,它是独立的。 If you change what would normally be keys of a JSON object to variables, you're making your "data format" extremely dependent on where it's going to be used. 如果您将JSON对象的键通常更改为变量,那么您的“数据格式”将极大地依赖于它将被使用的位置。 Variable name conflicts are almost guaranteed. 变量名称冲突几乎得到保证。 There's also no advantage, data in JSON objects is trivial to access already. 这也没有优势,JSON对象中的数据已经很容易访问。

Update: You could easily build this format by looping through your results, putting them into arrays and printing out Javascript code, it's not difficult. 更新:您可以通过循环结果,将它们放入数组并打印出Javascript代码来轻松构建此格式,这并不困难。 Despite that I'm not going to post the code here, since it's terrible practice. 尽管如此我不打算在这里发布代码,因为这是一种可怕的做法。 Instead, all you need to do is slightly change the code that will use this data: 相反,您只需稍微更改将使用此数据的代码:

// old
var html = '...' + key[index] + '...' +key2[index] + '...';

// new
var html = '...' + data[index].key + '...' + data[index].key2 + '...';

As for Chinese characters, Javascript/JSON is intrinsically UTF8, so it has no problems with Chinese characters. 至于中文字符,Javascript / JSON本质上是UTF8,因此它对汉字没有任何问题。 Just make sure your characters are UTF8 encoded when you package them as JSON. 当您将它们打包为JSON时,只需确保您的字符是UTF8编码的。

I can't help but suggest that you use a more elegant solution? 我不能不建议你使用更优雅的解决方案? Have you thought about using ajax? 你有没有想过使用ajax?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM