Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android json解析没有标记值_Android_Json_Parsing - Fatal编程技术网

android json解析没有标记值

android json解析没有标记值,android,json,parsing,Android,Json,Parsing,我需要使用以下代码解析JSON urlGetDeviceIdMobileNo = Global.URL + "device_id=" + strDeviceIdLocal; public class GetDeviceId extends AsyncTask<Void, Void, Void> { ProgressDialog progressDialog; @Override pro

我需要使用以下代码解析JSON

    urlGetDeviceIdMobileNo = Global.URL + "device_id="
                    + strDeviceIdLocal;

public class GetDeviceId extends AsyncTask<Void, Void, Void> {
        ProgressDialog progressDialog;

        @Override
        protected void onPreExecute() {
            progressDialog = new ProgressDialog(LoginScreen.this);
            progressDialog.setMessage("Please Wait...");
            progressDialog.setCancelable(false);
            progressDialog.show();
            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(Void... arg0) {
            JSONParser jParser = new JSONParser();
            JSONObject json = jParser.getJSONFromUrl(urlGetDeviceIdMobileNo);
            System.out.println("!!!!!!!!!!!! json==="+json);

            try {

                strDeviceIdServer = (String) json.getString(TAG_DEVICE_ID);
                strMobileNoServer = (String) json.getString(TAG_MOBILE_NO);

            } catch (JSONException e) {
                e.printStackTrace();
            }
return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            if (progressDialog.isShowing()) {
                progressDialog.dismiss();
            }
}
但logcat说:

  org.json.JSONException: No value for device_id
把这个换掉

try {

                strDeviceIdServer = (String) json.getString(TAG_DEVICE_ID);
                strMobileNoServer = (String) json.getString(TAG_MOBILE_NO);

            } catch (JSONException e) {
                e.printStackTrace();
            }

把这个换掉

try {

                strDeviceIdServer = (String) json.getString(TAG_DEVICE_ID);
                strMobileNoServer = (String) json.getString(TAG_MOBILE_NO);

            } catch (JSONException e) {
                e.printStackTrace();
            }


您的分析不正确。您的JSONObject有子JSONObject“data”,而这个子JSONObject包含设备id。因此,您必须像这样解析它:

try {
        JSONObject childJson = json.getJSONObject("data");
        if(childJson.has(TAG_DEVICE_ID))
            strDeviceIdServer = childJson.getString(TAG_DEVICE_ID);
        if(childJson.has(TAG_MOBILE_NO))
            strMobileNoServer = childJson.getString(TAG_MOBILE_NO);
    } catch (JSONException e) {
        e.printStackTrace();
    }

您的分析不正确。您的JSONObject有子JSONObject“data”,而这个子JSONObject包含设备id。因此,您必须像这样解析它:

try {
        JSONObject childJson = json.getJSONObject("data");
        if(childJson.has(TAG_DEVICE_ID))
            strDeviceIdServer = childJson.getString(TAG_DEVICE_ID);
        if(childJson.has(TAG_MOBILE_NO))
            strMobileNoServer = childJson.getString(TAG_MOBILE_NO);
    } catch (JSONException e) {
        e.printStackTrace();
    }

缺少,JSONObject obj=新的JSONObject(“数据”);然后String deviceId=obj.getString(“设备id”);将
device\u id
发布到服务器后,您是否也从
JSON
获取
device\u id
??而且你错过了你的“数据”标签。缺少,JSONObject obj=新的JSONObject(“数据”);然后String deviceId=obj.getString(“设备id”);将
device\u id
发布到服务器后,您是否也从
JSON
获取
device\u id
??你错过了你的“数据”标签。