【问题标题】:Getting exception: org.json.JSONObject cannot be converted to JSONArray获取异常:org.json.JSONObject 无法转换为 JSONArray
【发布时间】:2016-07-25 10:34:22
【问题描述】:

我是一名 android 初学者,请为此提供必要的帮助。

try {
 JSONArray jArray = new JSONArray(result);
 // Extract data from json and store into ArrayList as class objects
 for (int i = 0; i < jArray.length(); i++) {
  JSONObject json_data = jArray.getJSONObject(i);
  DataFish fishData = new DataFish();
  fishData.nonce = json_data.getString("nonce");
  fishData.iUserId = json_data.getString("iUserId");
  fishData.timestamp = json_data.getString("timestamp");
  fishData.token = json_data.getString("token");

  data.add(fishData);
 }

 // Setup and Handover data to recyclerview
 mRVFishPrice = (RecyclerView) findViewById(R.id.fishPriceList);
 mAdapter = new AdapterFish(PostActivity.this, data);
 mRVFishPrice.setAdapter(mAdapter);
 mRVFishPrice.setLayoutManager(new LinearLayoutManager(PostActivity.this));

} 
catch (JSONException e) {
 Toast.makeText(PostActivity.this, e.toString(), Toast.LENGTH_LONG).show();
}

我已将其转换为 JSONArray,但我仍在获取

org.json.JSONException: Value {"code":"0","message":"拒绝访问, org.json.JSONObject 类型的无效用户引用"} 不能是 转换为 JSONArray。

【问题讨论】:

  • 发布您的 JSON 数据。
  • 首先检查你的代码是o还是1。如果代码1获取数组值
  • 使用码键整数
  • 我已经在doinbackground方法中发布了JSON数据
  • 不,我的意思是发布您从服务器获得的 JSON 响应。

标签: android json android-asynctask


【解决方案1】:

这不是 JSONArray 对象。它是一个 JSONObject,你不能将它解析为 JSONArray。如需更多参考,您可以查看it here

【讨论】:

    【解决方案2】:

    试试吧。

    JSONArray alldata = new JSONArray();
    
    for(your codition to get data for how many array items in loop){
    JSONObject all = new JSONObject(); 
    all.put("nonce", "G9Ivek");
    all.put("iUserId", "477");
    all.put("timestamp","20160614060439");
    alldata.put(all);
    

    }

    并将所有数据发送到服务器。

    希望这会对你有所帮助..让我知道..

    发送到服务器

    JSONObject objMainList = new JSONObject();
    objMainList.put("ALLDETAILS", alldata);
    

    用于在 ASYNC TASK DOINBACKGROUND() 中与 api url 绑定日期

    Map<String, String> param = new HashMap<String, String>();
                        param.put("access_code", access_code);
                        param.put("auth_token", auth_token);
                        param.put("data", objMainList.toString());
    
                        try {
                            HttpUtility.sendPostRequest(Config.Submit_Answer, param);
                            String response = HttpUtility.readMultipleLinesRespone();
                            json = new JSONObject(response);
                            HttpUtility.disconnect();
    
    } catch (Exception e) {
    
                            e.printStackTrace();
    
                        }
    

    【讨论】:

    • 发送所有数据到服务器要写什么?
    • 请按照上面的答案发送到服务器..告诉我。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-07
    • 1970-01-01
    • 1970-01-01
    • 2021-07-12
    • 2021-11-20
    • 2015-08-11
    相关资源
    最近更新 更多