我尝试从AndroidStudio中的PHP脚本解析JSONArray。我从我的PHP脚本中得到以下JSON响应:
[{"0":"Firstname","meta_value":"Firstname"},{"0":"Lastname","meta_value":"Lastname"},{"valid":"1"},{"entered":"5"}]
现在,我尝试解析JSONArray以获取值meta_value,因此我想获取“姓氏”和“名字”。

但是我的代码中肯定有错误,因为我得到了响应:Exception: No value for meta_value

 public class SendPostRequest extends AsyncTask<String, Void, String> {
         String data ="";
         String dataParsed = "";
         String singleParsed ="";

         protected void onPreExecute(){}

         protected String doInBackground(String... arg0) {

             try {

                 URL url = new URL("https://sample.com/code2.php"); // here is your URL path
                 JSONObject postDataParams = new JSONObject();
                 postDataParams.put("sdata", scannedData);
                Log.e("params",postDataParams.toString());

                 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                 conn.setReadTimeout(15000 /* milliseconds */);
                 conn.setConnectTimeout(15000 /* milliseconds */);
                 conn.setRequestMethod("POST");
                 conn.setDoInput(true);
                 conn.setDoOutput(true);

                 OutputStream os = conn.getOutputStream();
                 BufferedWriter writer = new BufferedWriter(
                    new OutputStreamWriter(os, "UTF-8"));
                 writer.write(getPostDataString(postDataParams));

                 writer.flush();
                 writer.close();
                 os.close();

                 int responseCode=conn.getResponseCode();

                 if (responseCode == HttpsURLConnection.HTTP_OK) {

                     BufferedReader in=new BufferedReader(new
                        InputStreamReader(
                        conn.getInputStream()));

                     StringBuffer sb = new StringBuffer("");
                     String line="";

                     while((line = in.readLine()) != null) {
                    data = data + line;
                    sb.append(line);
                    break;
                     }



                     in.close();

                     JSONArray reader2 = new JSONArray(data);
                     for(int i =0 ;i <reader2.length(); i++){
                    JSONObject JO = (JSONObject) reader2.get(i);
                    singleParsed =  "Name:" + JO.get("meta_value") + "\n";

                    dataParsed = dataParsed + singleParsed +"\n" ;


                }





                return dataParsed;
            }
            else {
                return new String("false : "+responseCode);
            }
        }
        catch(Exception e){
            return new String("Exception: " + e.getMessage());
        }

    }

    @Override
    protected void onPostExecute(String result) {
        Toast.makeText(getApplicationContext(), result,
                Toast.LENGTH_LONG).show();
    }


}


请不要将我的答案标记为重复,因为我搜索了2周,但还没有找到答案!谢谢:)

最佳答案

我建议您使用GSON对JSON响应进行序列化和反序列化。这样就很容易处理。 GSON

样品:

new Gson().fromJson("YourJsonHere",JsonElement.class);


干杯塔里克

07-23 16:10