【问题标题】:parsing json file from server从服务器解析 json 文件
【发布时间】:2014-01-02 00:49:30
【问题描述】:

我将json 文件上传到我的服务器。这是json file,但是当我想解析它时,它给出了以下错误:

Error parsing data org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject

我应该在服务器中进行哪些设置?我只设置了MIME

您可以查看我的代码here

【问题讨论】:

  • 您可以打印您的回复吗?最好在此处添加您的代码。
  • 从 logcat 发布您的 json 响应

标签: android json getjson mime


【解决方案1】:

示例响应:

{"rows":[{"Fname":"rahim","Lname":"Durosimi","Predictions":"4","Cpredictions":"3","Points":"15"},{"Fname":"Otunba","Lname":"Alagbe","Predictions":"5","Cpredictions":"2","Points":"10"},{"Fname":"Olamide","Lname":"Jolaoso","Predictions":"4","Cpredictions":"2","Points":"10"},{"Fname":"g","Lname":"ade","Predictions":"1","Cpredictions":"1","Points":"5"},{"Fname":"Tiamiyu","Lname":"waliu","Predictions":"1","Cpredictions":"1","Points":"5"}]}

收到字符串响应后解析的等效代码。

JSONObject json = new JSONObject(content);          
JSONArray jArray = json.getJSONArray("rows");
            JSONObject json_data = null;
            for (int i = 0; i < jArray.length(); i++) {
                json_data = jArray.getJSONObject(i);
                String fname = json_data.getString("Fname");
String lname = json_data.getString("Lname");                
   }




<?php
header('Content-Type: application/json');
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "your url here"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
echo $output = curl_exec($ch); 
curl_close($ch);      
?>

【讨论】:

  • 我已经做到了,你看到我的代码了吗stackoverflow.com/questions/20574800/…
  • 哦,我明白了!一旦您从服务器获得的响应不完全是 json 数据,它就发生在我身上。我的情况是当我尝试使用 feedburner 将第三方网站的 xml 提要转换为 JSON 时。抱歉,我没有注意到开头的 我使用 php 创建 web 服务,它无痛只是 echo json_encode($var);对我来说,诀窍是使用 php 代理查看对可能解决方案的编辑。
猜你喜欢
  • 2017-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-02
  • 2023-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多