English 中文(简体)
如何在php中打印json响应
原标题:How print json response in php

我有来自远程站点的json对象。当我<code>vardump</code>json响应时。输出如下所示。。

object(GSResponse)#111 (7) {
  ["errorCode":"GSResponse":private]=>
  int(0)
  ["errorMessage":"GSResponse":private]=>
  NULL
  ["rawData":"GSResponse":private]=>
  string(1808) "{
  "UID": "*********",
  }
  ]
  }

如何使用php访问json响应中的rawData参数。是否有任何函数可以将其转换为php数组。

我感谢你的帮助。

最佳回答

已编辑-已更新以包含评论

Answer

假设$gsresponsevar是一个类型为gsresponse的对象,定义如下。

解码json响应-

$myjsonresponse= json_decode($gsresponsevar->getResponseText()) ;

或者检索var

echo $gsresponsevar->getString( uid );

Documentation

摘录自:http://developers.gigya.com/030_Server_SDKs/PHP/Reference/Class_GSResponse

string  getString(string $key [, string $defaultValue])
问题回答

这是通用的“无框架”原生方式

您可以使用JSON_decode解码JSON字符串

$json =  {"a":1,"b":2,"c":3,"d":4,"e":5} ;

$dataObject = json_decode($json);
$dataArray = json_decode($json, true);

第二个参数定义是获得对象(可通过$dataObject->;键访问)还是获得关联数组(可通过$dataArray[key]访问)。

注意API“示例#3使用json_decode()的常见错误”中提到的常见错误

这是Gigya-API的使用方式

查看Jason的回答以了解更多详细信息

$responseObject->getString( key );

您可以使用json_decode($json_array);

为了打印结果数组,您可以编写<code>var_dump(jsondecode($json_array))

Thankx。





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签