简体   繁体   English

使用PHP解析JSON(带有标签和数组的数组)

[英]Parse a JSON (array with tabs and arrays) with PHP

I have been trying to parse a JSON response since few days without success. 几天以来,我一直在尝试解析JSON响应,但未成功。 I've read on the internet and this site how to parse with a foreach but I think the structure of json is too different from the examples I've found. 我已经在互联网和该站点上阅读了如何使用foreach进行解析,但我认为json的结构与我发现的示例有很大不同。 So here is the JSON: 所以这是JSON:

 array(1) 

        { ["matches"]=> array(10) 
            { [0]=> array(13) { ["matchId"]=> int(2125878813) 
                                ["region"]=> string(3) "EUW" 
                                ["platformId"]=> string(4) "EUW1" 
                                ["matchMode"]=> string(7) "CLASSIC" 
                                ["matchType"]=> string(12) "MATCHED_GAME" 
                                ["matchCreation"]=> float(1432656547642) 
                                ["matchDuration"]=> int(1852) 
                                ["queueType"]=> string(15) "RANKED_SOLO_5x5" 
                                ["mapId"]=> int(11) 
                                ["season"]=> string(10) "SEASON2015" 
                                ["matchVersion"]=> string(9) "5.9.0.318" 
                                ["participants"]=> array(1) 
                                    { [0]=> array(10) { 
                                    ["teamId"]=> int(200) 
                                    ["spell1Id"]=> int(7) 
                                    ["spell2Id"]=> int(4) 
                                    ["championId"]=> int(67) 
                                    ["highestAchievedSeasonTier"]=> string(8) "PLATINUM" 
                                    ["timeline"]=> array(9) 
                                        { ["creepsPerMinDeltas"]=> array(3) 
                                            { ["zeroToTen"]=> float(6.2) 
                                              ["tenToTwenty"]=> float(7.4) 
                                              ["twentyToThirty"]=> float(4.4) } 
                                              ["xpPerMinDeltas"]=> array(3) 
                                                { ["zeroToTen"]=> float(336.2) 
                                                  ["tenToTwenty"]=> float(428.3) 
                                                  ["twentyToThirty"]=> float(396) } 
                                                    ["goldPerMinDeltas"]=> array(3) { 
                                                    ["zeroToTen"]=> float(270.7) 
                                                    ["tenToTwenty"]=> float(363.6) 
                                                    ["twentyToThirty"]=> float(294.8) }

If I understand well this is an array with tabs inside and arrays also ? 如果我理解得很好,这是一个内部带有制表符的数组,数组也可以?

I would like for example to display the 10 matches id : matches->matchId matches->participants->championId matches->participants->timeline->creepsPerMinDeltas->zeroToTen 例如,我想显示10个匹配ID:matches-> matchId match-> participants-> championId match-> participants-> timeline-> creepsPerMinDeltas-> zeroToTen

Here is what I've done so far to display the matchId : 到目前为止,我已经完成了显示matchId

 $decodeHisto = json_decode ($resultHisto,true);

    //var_dump($decodeHisto);       
    foreach ($decodeHisto{[matches]} as $d)      
    {

    echo $d->{[matchId]}-;

    }

Can anyone help me please ? 有人可以帮我吗?

Your code is not JSON, as JSON is exactly defined like this: 您的代码不是JSON,因为JSON的定义完全如下:

{
"Herausgeber": "Xema",
  "Nummer": "1234-5678-9012-3456",
  "Deckung": 2e+6,
  "Waehrung": "EURO",
  "Inhaber": {
    "Name": "Mustermann",
    "Vorname": "Max",
    "maennlich": true,
    "Hobbys": [ "Reiten", "Golfen", "Lesen" ],
    "Alter": 42,
    "Kinder": [],
    "Partner": null
  }
}

You can convert a JSON array to PHP with this code: 您可以使用以下代码将JSON数组转换为PHP:

$array = json_decode($json);

But as I said before, your code is not JSON. 但是正如我之前所说,您的代码不是JSON。 It seems to be even a PHP array. 它似乎甚至是一个PHP数组。 Then you can access with: 然后,您可以使用:

$arr["matches"][0]["matchId"]

for example. 例如。

Given the additional information you provided by posting an own "answer" to your question here I would like to point out this: 考虑到您通过在此处发布自己的“答案”来提供的其他信息,我想指出以下几点:

So if what you posted in your question is a dump of the result you got back from a call to json_decode() , then what you have simply is an array. 因此,如果您在问题中发布的内容是对调用json_decode()返回结果的转储,那么您所拥有的只是一个数组。 An associative, multi level array, but still just a normal php array. 关联的多级数组,但仍然只是普通的php数组。 This is also what the documentation of the json_decode() function clearly states: http://php.net/manual/en/function.json-decode.php 这也是json_decode()函数的文档明确指出的内容: http : json_decode()

There is absolutely no reason to somehow "parse" an array. 绝对没有理由以某种方式“解析”数组。 The "tabs" or whitespaces you can see in the dump are not part of the result you have, so of the array. 在转储中可以看到的“选项卡”或空格属于结果, 而是属于数组。 Those are artefacts by the dumoing function you use to visualize the array. 这些是您用来使阵列可视化的模拟功能的伪影。 You can simply access all members (or members of members...) of the array sirectly by their associative "path", as you will see in all examples of php code you can find on google. 您可以通过它们的关联“路径”直接直接访问数组的所有成员(或成员的成员...),正如您将在google上找到的所有PHP代码示例中所看到的那样。 Here an example: 这里是一个例子:

$value = $decodedJson['matches'][0]['matchDuration'];

Given $decodedJson is the result array as posted in your question, then $value will now hold the integer value 1852. 假设$decodedJson是问题中发布的结果数组,则$value现在将保留整数值1852。

You can access whatever part or element of the array you like. 您可以访问所需的数组的任何部分或元素。 you can iterate over parts of the array using php's iteration constructs like foreach and the like. 您可以使用诸如foreach之类的php迭代构造遍历数组的各个部分。 You can search in the array by means of php's array functions( http://php.net/manual/en/ref.array.php ). 您可以通过php的数组函数( http://php.net/manual/en/ref.array.php )在数组中进行搜索。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM