You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android JSON转JSONArray失败求助:网络请求JSON解析异常

Fixing Your JSON Parsing & Field Mismatch Issues

Hey there, let's get your code working smoothly—you've already nailed the hard part by successfully fetching the JSON data! Here's how to fix the two main issues you're facing:

1. Core Problem: Treating a Root JSONObject as a JSONArray

Your JSON starts with a root JSONObject that wraps the records array, but your current code tries to parse the entire response directly as a JSONArray. That's exactly why you're getting the conversion error.

Fix the Parsing Logic in onPostExecute

Update your code to first parse the root object, then extract the records array:

@Override
protected void onPostExecute(String result) {
    pdLoading.dismiss();
    List<Data> data = new ArrayList<>();
    
    try {
        // First parse the root JSONObject from the response string
        JSONObject rootJsonObject = new JSONObject(result);
        // Extract the "records" array from the root object
        JSONArray recordsArray = rootJsonObject.getJSONArray("records");

        // Loop through the records array to extract each item
        for(int i = 0; i < recordsArray.length(); i++){
            JSONObject userData = recordsArray.getJSONObject(i);
            Data fishData = new Data();
            
            fishData.did = userData.getString("id");
            fishData.demail = userData.getString("Email");
            // Fixed: Firstname → FirstName (matches JSON key)
            fishData.dfirst = userData.getString("FirstName");
            // Fixed: Lastname → LastName (matches JSON key)
            fishData.dlast = userData.getString("LastName");
            fishData.dpass = userData.getString("Password");
            fishData.dcat = userData.getString("Category");
            // Fixed: Description → Descrption (matches JSON key - note the missing 'i')
            fishData.ddesc = userData.getString("Descrption");
            fishData.dtag = userData.getString("Tagline");
            // Fixed: Experince → Experience (matches JSON key)
            fishData.dex = userData.getString("Experience");
            // Fixed: Currentwork → CurrentWork (matches JSON key)
            fishData.dcurrent = userData.getString("CurrentWork");
            fishData.dachieve = userData.getString("Achievements");
            fishData.dhobby = userData.getString("Hobbies");
            
            data.add(fishData);
        }

        // Set up RecyclerView with the parsed data
        mRVFishPrice = findViewById(R.id.recycle);
        mAdapter = new AdapterData(MainActivity.this, data);
        mRVFishPrice.setAdapter(mAdapter);
        mRVFishPrice.setLayoutManager(new LinearLayoutManager(MainActivity.this));
        
    } catch (JSONException e) {
        Toast.makeText(MainActivity.this, e.toString(), Toast.LENGTH_LONG).show();
    }
}

2. Fix Field Name Spelling Mismatches

You had several cases where your code's JSON key names didn't match the actual keys in your JSON file. These would have caused JSONException even after fixing the array/object issue. The fixes are marked in the code above, but here's a quick recap:

  • Firstname → FirstName
  • Lastname → LastName
  • Description → Descrption (your JSON uses this spelling—if it's a typo, you should update both the JSON and code to use Description for consistency)
  • Experince → Experience
  • Currentwork → CurrentWork

3. Network Connection Code Notes

Your changes to the network code are correct:

  • Adding conn.connect(); ensures the connection is explicitly opened
  • Commenting out conn.setDoOutput(true); is right because you're using a GET request (this setting is only needed for POST requests when sending data to the server)

Extra Tip for Robustness

To avoid crashes if a JSON field is missing or null, use optString() instead of getString()—it returns an empty string (or a default value you specify) instead of throwing an exception:

fishData.dfirst = userData.optString("FirstName", "");

内容的提问来源于stack exchange,提问作者Leo Algo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 07:25:59