使用PHP获取JSON数据

时间:2012-09-28 10:50:14

标签: php json stream

如何获取此方法转发到PHP网页的数据?

  URL url;
   HttpURLConnection connect = null;
   BufferedReader rd;
   StringBuilder sb;
   OutputStreamWriter wr;
   // Change this url to the url of your receiveJsonSms.php.
   String urlString = "http://www.paintedostrich.com/receiveJsonSms.php";
    try {
  System.setProperty("http.keepAlive", "false");
  url = new URL(urlString);
  connect = (HttpURLConnection) url.openConnection();
  connect.setRequestMethod("POST");
  connect.setDoOutput(true);
  connect.setDoInput(true);
  connect.setReadTimeout(10000);

  connect.connect();

  // write to the stream
  String data = URLEncoder.encode("texts", "UTF-8") + "="
          + URLEncoder.encode(jsonTexts.toString(), "UTF-8");

  wr = new OutputStreamWriter(connect.getOutputStream());
  wr.write(data);
  wr.flush();

  // read the result from the server
  rd = new BufferedReader(new InputStreamReader(connect.getInputStream()));
  sb = new StringBuilder();
  String line = null;
  while ((line = rd.readLine()) != null) {
    sb.append(line);
  }`

2 个答案:

答案 0 :(得分:0)

要在PHP中解码json,请使用json_decode()

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

var_dump(json_decode($json));
var_dump(json_decode($json, true));

?>

答案 1 :(得分:0)

我认为答案是

sb.ToString(); 

如果您想阅读JSON,那么您从服务器获取的数据就是

http://www.codeproject.com/Tips/397574/Use-Csharp-to-get-JSON-data-from-the-web-and-map-i