如何使用PHP

时间:2016-10-13 14:02:38

标签: php json

好的,所以我现在正在玩php。我有一个.html文件和一个.php文件。 .html文件包含textarea。 .php文件包含一些与用户键入的json字符串相混淆的函数。我知道如何将php输出到网页上。我想将整个json脚本输出到textarea。基本上在.php文件中,我想将json转换为字符串,并将该字符串传递给.html的textarea。 这是json:

    {
   "destination_addresses" : [ "New Town, Uckfield, East Sussex TN22 5DJ, UK" ],
   "origin_addresses" : [ "Maresfield, East Sussex TN22 2AF, UK" ],
   "rows" : [
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "3.0 mi",
                  "value" : 4855
               },
               "duration" : {
                  "text" : "22 mins",
                  "value" : 1311
               },
               "status" : "OK"
            }
         ]
      }
   ],
   "status" : "OK"
}

我尝试使用json_encode()但由于引号不合适而导致错误,所以我想知道是否有更简单的方法将整个json转换为字符串。

2 个答案:

答案 0 :(得分:0)

如果你没有获得行返回和格式化标签,你可以使用我的漂亮的json打印功能,它将为json数据添加制表符和行返回。

function PrettyJson($json) {
    $tc = 0;        //tab count
    $r = '';        //result
    $q = false;     //quotes
    $t = "\t";      //tab
    $nl = "\n";     //new line

    for($i=0;$i<strlen($json);$i++){
        $c = $json[$i];
        if($c=='"' && $json[$i-1]!='\\') $q = !$q;
        if($q){
            $r .= $c;
            continue;
        }
        switch($c){
            case '{':
            case '[':
                $r .= $c . $nl . str_repeat($t, ++$tc);
                break;
            case '}':
            case ']':
                $r .= $nl . str_repeat($t, --$tc) . $c;
                break;
            case ',':
                $r .= $c;
                if($json[$i+1]!='{' && $json[$i+1]!='[') $r .= $nl . str_repeat($t, $tc);
                break;
            case ':':
                $r .= $c . ' ';
                break;
            default:
                $r .= $c;
        }
    }
    return $r;
}

然后你就回应它

    echo '<textarea>'.PrettyJson( $json ).'</textarea>';

基本上它的作用是给出像这样的json字符串

    {"one":[1,2,3],"two":{"three":3}}

它会像这样(或类似的)

    {
        "one" : [1,2,3],
        "two" : {
              "three" : 3
        }
    }

通常情况下,我只是将它用于显示,所以如果你需要在解码时删除空白区域我不确定。

答案 1 :(得分:0)

如果PHP还包含HTML,则只需使用 KmsAlgorithm 来转义 static string bucketName = "*****************************"; static string keyName = "test.encrypted.bin"; static string uploadSourcePath = "c:\\temp\\test.bin"; static long partSize = 5 * 1024 * 1024; static String uploadId = ""; static void Main(string[] args) { if (checkRequiredFields()) { String cmkId = "************************************"; // Prepare our KMS client and kmsAlgorithm using (AmazonKeyManagementServiceClient kmsClient = new AmazonKeyManagementServiceClient()) using (KMSAlgorithm kmsAlgo = new KMSAlgorithm(kmsClient, cmkId)) { // Generate the encryption materials object with the algorithm object EncryptionMaterials encryptionMaterials = new EncryptionMaterials(kmsAlgo); // Now prepare an S3 crypto client using (AmazonS3EncryptionClient cryptoClient = new AmazonS3EncryptionClient(encryptionMaterials)) { // Initiate the multipart upload request specifying the bucket and key values InitiateMultipartUploadResponse initResp = cryptoClient.InitiateMultipartUpload( new InitiateMultipartUploadRequest() { BucketName = bucketName, Key = keyName }); uploadId = initResp.UploadId; long fileLength = new FileInfo(uploadSourcePath).Length; long contentLength = fileLength; long bytesRemaining = fileLength; List<PartETag> partETags = new List<PartETag>(); int partNumber = 0; while (bytesRemaining > 0) { long transferSize = bytesRemaining > partSize ? partSize : bytesRemaining; long partIndex = fileLength - bytesRemaining; partNumber++; UploadPartResponse resp = cryptoClient.UploadPart( new UploadPartRequest() { BucketName = bucketName, Key = keyName, FilePath = uploadSourcePath, FilePosition = partIndex, PartSize = transferSize, PartNumber = partNumber, UploadId = uploadId, IsLastPart = transferSize < AwsS3FileSystemSample1.Program.partSize }); partETags.Add( new PartETag( partNumber, resp.ETag )); bytesRemaining -= transferSize; } // Now complete the transfer CompleteMultipartUploadResponse compResp = cryptoClient.CompleteMultipartUpload( new CompleteMultipartUploadRequest() { Key = keyName, BucketName = bucketName, UploadId = initResp.UploadId, PartETags = partETags }); } } } Console.WriteLine("Press any key to continue..."); Console.ReadKey(); } 标记之间存在问题的特殊字符。

PHP文件的小例子:

htmlspecialchars()