16

jQueryAjaxを介してJSONとしてサーバーにデータを投稿する際に大きな問題が発生しました。JSLintは、データに問題がなく、リクエストのContent-Typeがに設定されていると言いますapplication/x-www-form-urlencoded; charset=UTF-8。サーバーはPHP5.2.11で実行されているため、使用できませんjson_last_error()

url_decode、utf8_decode、html_entities_decodeを試しましたが、何も機能しないようです。

var_dump(json_decode($jdata));nullを返しますが、var_dump($jdata)すべてを実行すると問題ないように見えます。$jdata投稿データです:$jdata = $this->input->post('requestdata');

ここに、Firebugから取得した投稿データの例がいくつかあります。

{
    "projectnumber": "345",
    "projecdescription": "345",
    "articles": [
        {
            "position": 1,
            "article_id": 677,
            "online_text": "3 Behälter; Band I-III nach indiv. Stückliste, Sprache: DE - Sprache: de"
        },
        {
            "position": 2,
            "article_id": 678,
            "online_text": "2 Behälter; Band I-III nach indiv. Stückliste, Sprache: ### - Sprache: en"
        }
    ]
}

編集:

私は今これを試しました:

$string = $this->input->post('requestdata');
var_dump($string);
$json = preg_replace('/,\s*([\]}])/m', '$1', utf8_encode($string));
$json = json_decode($json);
var_dump($json);

結果は次のとおりです。

string(338) "{" projectnumber ":" 4444 "、" projecdescription ":" 4444 "、" articles ":[{" position ":1、" article_id ":676、" online_text ":"###Behälter; バンドI-IIInachindiv。Stückliste、Sprache:DE-Sprache:de "}、{" position ":2、" article_id ":681、" online_text ":"###Behälter; バンドI-IIInachindiv。Stückliste、Sprache:###-Sprache:en "}]}" NULL

JSON文字列をPHPソースに直接貼り付けることで機能しますが、投稿から取得することはできません。

4

3 に答える 3

16

文字列の改行が原因でエラーが発生しています

$string = '{"projectnumber" : "4444","projecdescription" : "4444", "articles" : [{"position":1, "article_id" : 676, "online_text" : "### Behälter; Band I-III nach indiv. Stückliste, Sprache: DE 
 - Sprache: de"},{"position":2, "article_id" : 681, "online_text" : "### Behälter; Band I-III nach indiv. Stückliste, Sprache: ### 
 - Sprache: en"}]}';


$string = preg_replace("/[\r\n]+/", " ", $string);
$json = utf8_encode($string);
$json = json_decode($json);
var_dump($json);

出力

object(stdClass)[1]
  public 'projectnumber' => string '4444' (length=4)
  public 'projecdescription' => string '4444' (length=4)
  public 'articles' => 
    array
      0 => 
        object(stdClass)[2]
          public 'position' => int 1
          public 'article_id' => int 676
          public 'online_text' => string '### Behälter; Band I-III nach indiv. Stückliste, Sprache: DE   - Sprache: de' (length=78)
      1 => 
        object(stdClass)[3]
          public 'position' => int 2
          public 'article_id' => int 681
          public 'online_text' => string '### Behälter; Band I-III nach indiv. Stückliste, Sprache: ###   - Sprache: en' (length=79)
于 2012-10-16T10:29:53.963 に答える
11

改行にも投票する

json_decode_nice +改行を保持:

function json_decode_nice($json, $assoc = TRUE){
    $json = str_replace("\n","\\n",$json);
    $json = str_replace("\r","",$json);
    $json = preg_replace('/([{,]+)(\s*)([^"]+?)\s*:/','$1"$3":',$json);
    $json = preg_replace('/(,)\s*}$/','}',$json);
    return json_decode($json,$assoc);
}

改行を保持したい場合は、スラッシュをエスケープしてください。

すべてがutf-8(ヘッダー、データベース接続など)に設定されている場合は、utf-8エンコードは必要ありません。

于 2013-04-30T23:22:03.713 に答える
2
$string = preg_replace("/[\r\n]+/", " ", $string);
$json = utf8_encode($string);
$json = json_decode($json);
var_dump($json);
于 2016-01-21T17:46:32.377 に答える