How to change the JSON output format and how to support the Chinese character?

I am currently using the following code to output my JSON output from MySQL.

<?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 result:

<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 of how I can change my output in a format like:

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

What I will use later with the 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 does my json output support Chinese character?

Thanks for the help and reading!

+3
source share
2 answers

It’s best to guess your question:

How to change this JSON format:

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

In this format:

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

: . JSON - , . , JSON , " " , . . , JSON .

Update: , , Javascript, . , , . , , , :

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

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

, Javascript/JSON UTF8, . , UTF8 JSON.

0

, ? ajax?

0

Source: https://habr.com/ru/post/1741885/


All Articles