首页 \ 问答 \ 如何将JSON放入PHP变量?(How do I put JSON into a PHP Variable?)

如何将JSON放入PHP变量?(How do I put JSON into a PHP Variable?)

我试图把JSON-Information放到一个PHP变量中,我一直都是错误的。

这是我使JSON可读的代码:

header('Content-Type: application/json');
$jsondecode1 = json_decode($json1);
print_r ($jsondecode1);

这是$jsondecode1一个片段:

Array
(
    [0] => stdClass Object
        (
            [flightId] => ******
            [******] => stdClass Object
                (
                    [******] => ******
                    [******] => ******
                    [******] => ******
                    [******] => ******
                    [******] => ******                
                )

注意:当我echojsondecode1它会输出:

在第48行的C:\ xampp ...... \ simpletest3.php中进行字符串转换

所以我使用了print_r()

我试图把(例如) [flightId]放入一个PHP变量中:

$jsondecode1 = json_decode($json1);
$jsondecode2 = $jsondecode1->flightId;
print_r ($jsondecode2);

输出: 注意 :尝试在C中获取非对象的属性'flightId' 48行的......... \ simpletest3.php

我也尝试了其他一些代码,但输出非常相似,我不想让我的问题比它需要的时间更长。 那么,如何将(例如) [flightId]放入PHP变量中。

编辑:

解:

$jsondecode1 = json_decode($json1);
$jsondecode2 = $jsondecode1[0]->flightId;
print_r ($jsondecode2);

要么:

foreach ($jsondecode1 as $data) {
    print_r($data->flightId);
}

I´m trying to put JSON-Information into a PHP variable and I´m getting Errors all the time.

This is my Code to make the JSON readable:

header('Content-Type: application/json');
$jsondecode1 = json_decode($json1);
print_r ($jsondecode1);

This is a snippet of the $jsondecode1:

Array
(
    [0] => stdClass Object
        (
            [flightId] => ******
            [******] => stdClass Object
                (
                    [******] => ******
                    [******] => ******
                    [******] => ******
                    [******] => ******
                    [******] => ******                
                )

Notice: When I echo the jsondecode1 it outputs this:

Array to string conversion in C:\xampp......\simpletest3.php on line 48

So I used print_r().

What I tried to put (for example) [flightId] into a PHP Variable:

$jsondecode1 = json_decode($json1);
$jsondecode2 = $jsondecode1->flightId;
print_r ($jsondecode2);

Output: Notice: Trying to get property 'flightId' of non-object in C:.........\simpletest3.php on line 48

I tried some other codes too, but the Outputs were very similar and I don´t want to make my Question longer than it needs to be. So, how do I put (for example) the [flightId] into a PHP variable.

Edit:

Solution:

$jsondecode1 = json_decode($json1);
$jsondecode2 = $jsondecode1[0]->flightId;
print_r ($jsondecode2);

Or:

foreach ($jsondecode1 as $data) {
    print_r($data->flightId);
}

原文:https://stackoverflow.com/questions/47991040
更新时间:2020-05-08 20:23

相关问答

更多

如何把这个Json数据放入一个PHP变量中?(How to put this Json Data into a Php Variable?)

您可能会发现将JSON字符串解码为PHP关联数组而不是对象会更简单: $jsondecode2 = json_decode($htmlj, true); $Country = $jsondecode2['predictions'][0]['structured_formatting']['secondary_text']; json_decode函数的第二个选项告诉它返回一个数组而不是一个对象。 更多信息在这里: http : //php.net/manual/en/function.json- ...

从JSON获取PHP变量[重复](Get variable with PHP from JSON [duplicate])

这应该工作: $get_weather = $jsonobj->main[0]->weather; This should work: $get_weather = $jsonobj->main[0]->weather;

PHP获取值然后将其放入变量(PHP Get value then put it to a variable)

如果你不想为$xp1 , $xp2等使用数组,你总是可以使用变量变量 : $xml=simplexml_load_file("d2ladder.xml") or die("Error: Cannot create object"); $xp1 = 4439; $xp2 = 8439; // And so on ... for ($i = 0; $i < 100; $i++) { $xp = $xml->ladder[12]->char[$i]->experience; $xppe ...

json作为php变量(json as php variable)

$data = curl_exec($curl); //Will get the json data $data = json_decode($data,TRUE); //will decode the json into associative arrays echo $data['place_id']; 这应该做你想要完成的事情。 试过。 评论是否不起作用。 $data = curl_exec($curl); //Will get the json data $data = ...

PHP将变量放入SimpleXML中(PHP Put variable in SimpleXML)

解决方案是使用intval()将对象转换为整数 $num = intval($categorie['id']); $fav = $xml->categorie[$num]->addChild("fav"," "); $fav->addChild("link", $link); $fav->addChild("desc", $desc); $xml->asXML('../data/data.xml'); The solution is to convert an object to integer ...

PHP从json结果中获取变量(PHP get variable from json results)

$res = json_decode($data,true); $my_text = $res['text_block'][0]['text']; 现在你可以使用my_text作为变量 $res = json_decode($data,true); $my_text = $res['text_block'][0]['text']; now you can use my_text as a variable

json结果中的javascript对象中的php变量(php variable in a javascript object within a json result)

您将不得不使用连接字符串. 并删除索引的引号转义,如下所示: $result["html"] .= "<script type='text/javascript'>setTimeout(function () { window.location='" . $config["BURL"] . "'; }, 2500);</script>"; You will have to concatenate strings using . and remove the escaping of the quo ...

获取json数据并将其放入变量中(Get json data and put it into a variable)

如果您知道要加载的类的类型,请考虑使用NUGET包NewtonSoft.Json。 使用此包时,在一个语句中转换为来自JSON字符串的对象。 例: 使用Visual Studio创建控制台应用程序JSonSaveLoad 安装NewtonSoft.Json: 在解决方案资源管理器中,右键单击项目的引用,然后选择“管理NUGET包”。 搜索Json.Net并安装它 创建一个包含一些简单属性的类: public class Person { public string ...

如何将表单中的选定选项放入php变量?(How to put selected option in a form into a php variable?)

首先给你的选择框命名。 然后根据您的要求在同一文件或其他文件中使用$_POST 。 <?php if(isset($_POST['abc'])){ $selected=$_POST['abc']; } ?> <form action="" method="post" class="text-center"> <select class="form-control" name="abc"> <option value="work1">1 hours</option> <opti ...

如何将php响应放入i javascript变量?(How Can I put a php response into i javascript variable?)

您正在寻找的是XMLHttpRequest ,它可以从JavaScript代码启动HTTP请求; 不要注意名称中误导性的XML 。 所有现代JavaScript库都提供高级函数来增强此函数的功能(例如jQuery.ajax )。 What you are looking for is XMLHttpRequest, which enables to initiate HTTP requests from JavaScript code; don't pay attention to the mis ...

相关文章

更多

最新问答

更多
  • iOS中导致此内存泄漏的原因(What is causing this memory leak in iOS)
  • 你为什么更喜欢:要求:参考:全部:在clojure中使用(Why should you prefer :require :refer :all over :use in clojure)
  • 将表行解析为嵌套集合(Parsing table rows into nested collections)
  • java.io.IOException:由peer重置连接(java.io.IOException: Connection reset by peer)
  • 使用Python list-comprehension从嵌套的tuple-dictionary结构中检索数据(Retrieve data from nested tuple-dictionary structure using Python list-comprehension)
  • 查找树中两个节点之间的(保证唯一)路径(Finding the (guaranteed unique) path between two nodes in a tree)
  • 我想添加一个回到上一页的php文档的链接(I want to add a link to php document that goes back to previous page)
  • Python并行线程(Python parallel threads)
  • Kentico的Field数据类型Date也显示时间(Kentico's Field data type Date also showing time)
  • android google maponClick没有响应(android google maponClick not responsive)
  • setDisplayHomeAsUpEnabled关闭ActionBarDrawerToggle(setDisplayHomeAsUpEnabled closing ActionBarDrawerToggle)
  • 确定用户是否运行TWRP或CWM恢复(Determine if user runs TWRP or CWM recovery)
  • 当删除按钮已经执行了sql时,我如何刷新此页面(称为add_user.php)(How i can refresh this page (called add_user.php) when delete button already executed the sql too)
  • InfluxDB平均每周的一天(InfluxDB average for day of the week)
  • 对象p5.js的声音效果(Sound effect on objects p5.js)
  • 如何提高自动载入性能(How increase autoload performance)
  • Asp.net Webapi使用[FromUri]和Route有两个参数(Asp.net Webapi using [FromUri] with Route that has two parameter)
  • IE7和HTML上的显示块严格(IE7 and display block on HTML strict)
  • HTML附加异步[关闭](HTML appending Async [closed])
  • 在Windows Phone 7移动网站上平滑滚动(Smooth scrolling on windows Phone 7 mobile site)
  • JavaScript不承认PHP变量[关闭](javascript not recognizing php variables [closed])
  • 更改按钮功能时出现未捕获参考错误(Getting Uncaught Reference Error when changing the function of a button)
  • ZeroMQ无法在两个Docker容器之间进行通信(ZeroMQ failing to communicate between two Docker containers)
  • 为什么字符串索引(尽管表示整数)在数组中有效?(Why do string indices (albeit representing integers) work in arrays?)
  • 为什么要实现IDisposable来清理非托管资源?(Why should you implement IDisposable to clean up unmanaged resources only?)
  • 将文件上传到服务器,然后附加到电子邮件并发送一个php文件(Upload file to server, then attach to email and send in one php file)
  • 从PHP脚本获取数据(Getting data back from a PHP script)
  • 在ui:repeat中使用inputText来更新值的ArrayList不起作用[重复](Using inputText inside ui:repeat to update ArrayList of values is not working [duplicate])
  • Salt - virtualenv的多个需求文件(Salt - multiple requirement files to virtualenv)
  • 一个Web角色上的两个Web应用程序 - 配置问题(Two web applications on one Web Role - config issue)