簡體   English   中英

用PHP獲取JSON數據

[英]Getting JSON data with PHP

如何獲取此方法轉發到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);
  }`

要在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));

?>

我認為答案是

sb.ToString(); 

如果您想讀取JSON,則從服務器獲取的多數民眾贊成在數據中使用

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM