2012-10-16 61 views
12

Tôi gặp vấn đề lớn khi đăng dữ liệu qua jQuery Ajax dưới dạng JSON tới Máy chủ của tôi. JSLint nói rằng dữ liệu là OK và Content-Type của yêu cầu được đặt thành application/x-www-form-urlencoded; charset=UTF-8. Máy chủ chạy trên PHP 5.2.11 vì vậy tôi không thể sử dụng json_last_error().Json_decode với các ký tự đặc biệt

Tôi đã thử url_decode, utf8_decode và html_entities_decode, nhưng không có gì có vẻ hiệu quả.

var_dump(json_decode($jdata)); trả về giá trị rỗng, nhưng nếu tôi làm var_dump($jdata) mọi thứ đều OK. $jdata là dữ liệu bài đăng: $jdata = $this->input->post('requestdata');.

Dưới đây một số dữ liệu ví dụ bài lấy từ 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" 
     } 
    ] 
} 

Edit:

tôi đã cố gắng này ngay bây giờ:

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

Kết quả là:

string (338) "{" projectnumber ":" 4444 "," projecdescription ":" 4444 "," bài viết ": [{" vị trí ": 1," article_id ": 676," online_text ":" ### Behälter; Ban nhạc I-III nach indiv. Stückliste, Sprache: DE - Sprache: de "}, {" vị trí ": 2," article_id ": 681," online_text ":" ### Behälter; Ban nhạc I-III nach indiv. Stückliste, Sprache: ### - Sprache: en "}]}" NULL

Bằng cách dán chuỗi JSON đạo vào nguồn PHP nó hoạt động, nhưng nhận được nó từ bài không!

+0

Điều này sẽ làm việc cho bạn: http://stackoverflow.com/ a/12884807/1226894 – Baba

+0

có thể trùng lặp của [json \ _decode trả về null trong php] (http://stackoverflow.com/questions/12884802/json-decode-is-returning-null-in-php) – Baba

+0

Bạn có chắc chắn không rằng bạn có một chuỗi UTF8 ở phía máy chủ? Hãy thử 'var_dump (json_decode (utf8_encode ($ jdata)));' – ThiefMaster

Trả lời

9

Bạn đang có lỗi vì dòng mới trong chuỗi của bạn

$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); 

Output

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) 
+2

bạn đã thực hiện một ngày của tôi! – fillibuster

+0

@grolle tốt để biết .. cảm ơn :) – Baba

9

Bỏ phiếu cho newline quá

json_decode_nice + giữ linebreaks:

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); 
} 

Nếu bạn muốn giữ đường kẻ chỉ cần thoát khỏi dấu gạch chéo.

Bạn không cần phải utf-8 mã hóa, nếu tất cả mọi thứ được thiết lập để utf-8 (tiêu đề, kết nối db, vv)

+2

Đừng quên bao gồm \ t trong danh sách các nhân vật nghịch ngợm.Điều này chỉ có vẻ là một vấn đề đối với PHP <5.4. – mtutty

+0

@mtutty, cảm ơn nhận xét đó! – Hafenkranich

+1

@RaphaelWeber Cảm ơn Bro bạn đã tiết kiệm thời gian của tôi –

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