使用javascript通过POST方法从JSON获取数据

时间:2016-11-03 06:57:40

标签: json post

问题是当我的PHP文件获取Method post发送的数据时:

$play=$_POST['players'];

结果是:

"[{'type_player': 'Hombres Bestia' ,'mo': 6, 'st': 3, 'de': 3, 'ar': 8, 'N': 'FGM' , 'D': 'AP' , 'price': 60000, 'habs': [20], 'max_number': 16 }, {'type_player': 'Guerreros del Caos' ,'mo': 5, 'st': 4, 'de': 3, 'ar': 9, 'N': 'FGM' , 'D': 'AP' , 'price': 100000, 'habs': [], 'max_number': 4 }, {'type_player': 'Minotauro' ,'mo': 5, 'st': 5, 'de': 2, 'ar': 8, 'N': 'FM' , 'D': 'AGP' , 'price': 150000, 'habs': [3,15,20,32,34,71], 'max_number': 1 }, {'type_player': 'qwe' ,'mo': 2, 'st': 3, 'de': 4, 'ar': 5, 'N': 'P' , 'D': 'M' , 'price': 132, 'habs': [0], 'max_number': 2 }]"

当我json_decode,

$players=json_decode($play,true);

它返回:

[{'type_player': 'Hombres Bestia' ,'mo': 6, 'st': 3, 'de': 3, 'ar': 8, 'N': 'FGM' , 'D': 'AP' , 'price': 60000, 'habs': [20], 'max_number': 16 }, {'type_player': 'Guerreros del Caos' ,'mo': 5, 'st': 4, 'de': 3, 'ar': 9, 'N': 'FGM' , 'D': 'AP' , 'price': 100000, 'habs': [], 'max_number': 4 }, {'type_player': 'Minotauro' ,'mo': 5, 'st': 5, 'de': 2, 'ar': 8, 'N': 'FM' , 'D': 'AGP' , 'price': 150000, 'habs': [3,15,20,32,34,71], 'max_number': 1 }, {'type_player': 'qwe' ,'mo': 2, 'st': 3, 'de': 4, 'ar': 5, 'N': 'P' , 'D': 'M' , 'price': 132, 'habs': [0], 'max_number': 2 }] 

问题在于,当我尝试在php中获取任何内容时,我无法做到。例如:

echo "mo:".$players[0]['mo'];

它返回:

mo:[

任何帮助?

正如你所说,我已经改变了发送数据的方式,现在当我$ _POST它返回时:

[{\"type_player\": \"Hombres Bestia\" ,\"mo\": 6, \"st\": 3, \"de\": 3, \"ar\": 8, \"N\": \"FGM\" , \"D\": \"AP\" , \"price\": 60000, \"habs\": [20], \"max_number\": 16 }, {\"type_player\": \"Guerreros del Caos\" ,\"mo\": 5, \"st\": 4, \"de\": 3, \"ar\": 9, \"N\": \"FGM\" , \"D\": \"AP\" , \"price\": 100000, \"habs\": [], \"max_number\": 4 }, {\"type_player\": \"Minotauro\" ,\"mo\": 5, \"st\": 5, \"de\": 2, \"ar\": 8, \"N\": \"FM\" , \"D\": \"AGP\" , \"price\": 150000, \"habs\": [3,15,20,32,34,71], \"max_number\": 1 }, {\"type_player\": \"qdwqw\" ,\"mo\": 2, \"st\": 2, \"de\": 2, \"ar\": 1, \"N\": \"P\" , \"D\": \"F\" , \"price\": 13244, \"habs\": [0], \"max_number\": 1 }]"

我解码它并返回:

[{"type_player": "Hombres Bestia" ,"mo": 6, "st": 3, "de": 3, "ar": 8, "N": "FGM" , "D": "AP" , "price": 60000, "habs": [20], "max_number": 16 }, {"type_player": "Guerreros del Caos" ,"mo": 5, "st": 4, "de": 3, "ar": 9, "N": "FGM" , "D": "AP" , "price": 100000, "habs": [], "max_number": 4 }, {"type_player": "Minotauro" ,"mo": 5, "st": 5, "de": 2, "ar": 8, "N": "FM" , "D": "AGP" , "price": 150000, "habs": [3,15,20,32,34,71], "max_number": 1 }, {"type_player": "qdwqw" ,"mo": 2, "st": 2, "de": 2, "ar": 1, "N": "P" , "D": "F" , "price": 13244, "habs": [0], "max_number": 1 }] 

现在,ith应该可以工作,但是当我尝试获取数据时,结果是一样的...进一步的答案? 提前谢谢。

2 个答案:

答案 0 :(得分:0)

您的json无效,因为json值用双引号括起来而不是单引号

注意:字符串应该用双引号括起来。

    <?php

    $play='[{"type_player": "Hombres Bestia" ,"mo": 6, "st": 3, "de": 3, "ar": 8, "N": "FGM" , "D": "AP" , "price": 60000, "habs": [20], "max_number": 16 }, {"type_player": "Guerreros del Caos" ,"mo": 5, "st": 4, "de": 3, "ar": 9, "N": "FGM" , "D": "AP" , "price": 100000, "habs": [], "max_number": 4 }, {"type_player": "Minotauro" ,"mo": 5, "st": 5, "de": 2, "ar": 8, "N": "FM" , "D": "AGP" , "price": 150000, "habs": [3,15,20,32,34,71], "max_number": 1 }, {"type_player": "qwe" ,"mo": 2, "st": 3, "de": 4, "ar": 5, "N": "P" , "D": "M" , "price": 132, "habs": [0], "max_number": 2 }]';

    $players=json_decode($play,true);

    echo "<pre>";
    print_r($players);

    echo $players[0]['mo'];

    ?>

输出:

        Array
    (
        [0] => Array
            (
                [type_player] => Hombres Bestia
                [mo] => 6
                [st] => 3
                [de] => 3
                [ar] => 8
                [N] => FGM
                [D] => AP
                [price] => 60000
                [habs] => Array
                    (
                        [0] => 20
                    )

                [max_number] => 16
            )

        [1] => Array
            (
                [type_player] => Guerreros del Caos
                [mo] => 5
                [st] => 4
                [de] => 3
                [ar] => 9
                [N] => FGM
                [D] => AP
                [price] => 100000
                [habs] => Array
                    (
                    )

                [max_number] => 4
            )

        [2] => Array
            (
                [type_player] => Minotauro
                [mo] => 5
                [st] => 5
                [de] => 2
                [ar] => 8
                [N] => FM
                [D] => AGP
                [price] => 150000
                [habs] => Array
                    (
                        [0] => 3
                        [1] => 15
                        [2] => 20
                        [3] => 32
                        [4] => 34
                        [5] => 71
                    )

                [max_number] => 1
            )

        [3] => Array
            (
                [type_player] => qwe
                [mo] => 2
                [st] => 3
                [de] => 4
                [ar] => 5
                [N] => P
                [D] => M
                [price] => 132
                [habs] => Array
                    (
                        [0] => 0
                    )

                [max_number] => 2
            )

    )

    6

更新2:

您只需将以下链接中的以下代码复制粘贴即可查看正确的解决方案http://phpfiddle.org/

        <?php
        $play="[{\"type_player\": \"Hombres Bestia\" ,\"mo\": 6, \"st\": 3, \"de\": 3, \"ar\": 8, \"N\": \"FGM\" , \"D\": \"AP\" , \"price\": 60000, \"habs\": [20], \"max_number\": 16 }, {\"type_player\": \"Guerreros del Caos\" ,\"mo\": 5, \"st\": 4, \"de\": 3, \"ar\": 9, \"N\": \"FGM\" , \"D\": \"AP\" , \"price\": 100000, \"habs\": [], \"max_number\": 4 }, {\"type_player\": \"Minotauro\" ,\"mo\": 5, \"st\": 5, \"de\": 2, \"ar\": 8, \"N\": \"FM\" , \"D\": \"AGP\" , \"price\": 150000, \"habs\": [3,15,20,32,34,71], \"max_number\": 1 }, {\"type_player\": \"qdwqw\" ,\"mo\": 2, \"st\": 2, \"de\": 2, \"ar\": 1, \"N\": \"P\" , \"D\": \"F\" , \"price\": 13244, \"habs\": [0], \"max_number\": 1 }]";


        $players=json_decode($play,true);

        echo "<pre>";
        print_r($players);

        ?>

答案 1 :(得分:0)

试试这个会起作用:

JSON 表示法是用双引号编写的。另请注意,JSON data exchange format确实需要围绕标识符使用双引号。

<?php
$play = '[
  { 
    "type_player": "Hombres Bestia" ,
    "mo": 6, 
    "st": 3, 
    "de": 3, 
    "ar": 8, 
    "N": "FGM" , 
    "D": "AP" , 
    "price": 60000, 
    "habs": [20], 
    "max_number": 16 
  },
  {
    "type_player": "Guerreros del Caos" ,
    "mo": 5, 
    "st": 4, 
    "de": 3, 
    "ar": 9, 
    "N": "FGM" , 
    "D": "AP" , 
    "price": 100000, 
    "habs": [], "max_number": 4 
  },
  { 
    "type_player": "Minotauro" ,
    "mo": 5, 
    "st": 5, 
    "de": 2, 
    "ar": 8, 
    "N": "FM" , 
    "D": "AGP" , 
    "price": 150000, 
    "habs": [3,15,20,32,34,71], 
    "max_number": 1 
  }, 
  {
    "type_player": "qwe" ,
    "mo": 2, 
    "st": 3, 
    "de": 4, 
    "ar": 5, 
    "N": "P" , 
    "D": "M" , 
    "price": 132, 
    "habs": [0], 
    "max_number": 2 
    }
]';

$players = json_decode($play,true);

echo $players[0]['mo'];

?>

输出 6