找到你要的答案

Q:org.json.JSONException: No value for opening_hours

Q:org.json.jsonexception:为opening_hours没有价值

I am fetching following JSON :

{
   "html_attributions" : [
      "Listings by \u003ca href=\"http://www.indiacom.com/\"\u003eIndiacom Yellow Pages\u003c/a\u003e"
   ],
   "next_page_token" : "CoQC-wAAAJX-ZD5lgNVPx6qwHSGS8N2CxEWUUPk8W479KD-36_ADA-1IP-feLNGdIJRKLE8LSi9csSI0NQ6qmN1d3-2bM9RA940Hcph4xUwigi3RKLRk_F0a8QjwutNfED8cHD2YVV2TxSrU1FeGbOixx3nC_70Ynq7bfL7t8CvUrxeLa7ljTyksmkuaIXFYhgKHCACufqk_fPSbrCEP0JvyN-FfAClaY9ucK6EcZhu1E2aC8LkkNvrmPSSUgcX7hOG6TywZTvCspUpktgXUN-Qd2WFgFqKwlOLOSm4R_GLCJzxS4WIu98vDh3dT7c6x7KP5IQDLRznrATglQ_gPkqUtSE5fFcMSEO7OF0BbRqVXQeILMLfp8xgaFPl6zJI-UvrQ0lL93nIkjsIDsukd",
   "results" : [
      {
         "geometry" : {
            "location" : {
               "lat" : 30.73732,
               "lng" : 76.785848
            }
         },
         "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/jewelry-71.png",
         "id" : "9e3b67275d7c32b85c301d1ad708519ad11bd5e4",
         "name" : "Tanishq",
         "opening_hours" : {
            "open_now" : true,
            "weekday_text" : []
         },
         "place_id" : "ChIJx3CfWf9mUjoRbOxyCwd6Ujg",
         "reference" : "CmRaAAAAAHqTGUQISzXGLMXzxqDCn29rSuDc-vBCOczSdGVZwxBaNkVE2sFEoYio7HpH6KzAYkOgkQGKHb7tKVSk2_XYObCLmvIpLT57GFUQhr0l8uWNabXWjkco79L6kyEJyC_nEhCObWMxWqub2GZ0ZXanC6msGhRUdmwS-FFoNL-EMeyKDF1w-P0lrg",
         "scope" : "GOOGLE",
         "types" : [ "jewelry_store", "store", "establishment" ],
         "vicinity" : "Himalaya Marg, Sector 17, Chandigarh"
      },
      {
         "geometry" : {
            "location" : {
               "lat" : 30.734622,
               "lng" : 76.76966
            }
         },
         "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/jewelry-71.png",
         "id" : "fec37080b9573fd4c98dfa8effe0be741362451f",
         "name" : "Jain Jewellers",
         "opening_hours" : {
            "open_now" : true,
            "weekday_text" : []
         },
         "photos" : [
            {
               "height" : 475,
               "html_attributions" : [],
               "photo_reference" : "CpQBggAAAGCpQElDqh0LsnCZYa2IOP8JNW_a0_1-VgCHsF-HlZjPeUyWS05j99LwPnBrkp6-Iz9HLxEJ3MQtJjJ39fudLldi9HeyGYpmI5zspYP7cFORcBTQJbT-yqw2Wf1a3sT1d1ry3GLn0m443DsDZgTSbN-Kmq91eZ2bfR30g3VXyFqUpNPrSAbDHQxplC9qrfmvTRIQ3RQjvxNNtFh02cx3xlxsWhoUL5yDyapLNkWo9t3mNldvdsyBk1o",
               "width" : 475
            }
         ],
         "place_id" : "ChIJzaqqqqXtDzkRv6HcbBzPtyM",
         "reference" : "CnRhAAAAwbGT9XUseXle30IC6PRphi6ekxeQkL4s9K_S6M8NUJAZn-mI9LgdXW_muYuaDE34nDNpJcDUMX4jgYqsgreIBETH3aC1cZhu0GRHakYOSyWw5Z2TtflGwOtkNqaI1phZ1C14tIlVf2gN5FtWdN4XLBIQbGGCgCEi2wj5EC3H5BzbqRoUGQ4QJzKXOnXSMlSXMR5PokdY9JE",
         "scope" : "GOOGLE",
         "types" : [ "finance", "jewelry_store", "store", "establishment" ],
         "vicinity" : "SCF 8, SECTOR 22 D, Chandigarh"
      },

This is a part of json . I am getting problem on fetching opening_hours. It says the following item does not exist but it is coming in the K. Here is my json code :

JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
contacts = jsonObj.getJSONArray("results");

// looping through All Contacts
for (int i = 0; i < contacts.length(); i++) {
    JSONObject c = contacts.getJSONObject(i);

    String name = c.getString("name");
    names.add(name);
    Log.v("name is", name);
    // cat.add(catagory);
    String vicinity = c.getString("vicinity");
    Log.v("vicinity is", vicinity);
    location.add(vicinity);
    // link.add(img_link);

    String open = c.getString("opening_hours");

For few results value is coming for open string but at one point it says value does not exist and the the code stops there.

Here is the logcat :

05-04 18:33:01.451: W/System.err(3695): org.json.JSONException: No value for opening_hours
05-04 18:33:01.461: W/System.err(3695):     at org.json.JSONObject.get(JSONObject.java:354)
05-04 18:33:01.461: W/System.err(3695):     at org.json.JSONObject.getString(JSONObject.java:510)
05-04 18:33:01.461: W/System.err(3695):     at com.app.jewel.HomeFragment$GetJewelry.doInBackground(HomeFragment.java:153)
05-04 18:33:01.461: W/System.err(3695):     at com.app.jewel.HomeFragment$GetJewelry.doInBackground(HomeFragment.java:1)
05-04 18:33:01.461: W/System.err(3695):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
05-04 18:33:01.461: W/System.err(3695):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
05-04 18:33:01.461: W/System.err(3695):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
05-04 18:33:01.471: W/System.err(3695):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
05-04 18:33:01.471: W/System.err(3695):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
05-04 18:33:01.471: W/System.err(3695):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
05-04 18:33:01.471: W/System.err(3695):     at java.lang.Thread.run(Thread.java:856)

Please help

我取以下JSON:

{
   "html_attributions" : [
      "Listings by \u003ca href=\"http://www.indiacom.com/\"\u003eIndiacom Yellow Pages\u003c/a\u003e"
   ],
   "next_page_token" : "CoQC-wAAAJX-ZD5lgNVPx6qwHSGS8N2CxEWUUPk8W479KD-36_ADA-1IP-feLNGdIJRKLE8LSi9csSI0NQ6qmN1d3-2bM9RA940Hcph4xUwigi3RKLRk_F0a8QjwutNfED8cHD2YVV2TxSrU1FeGbOixx3nC_70Ynq7bfL7t8CvUrxeLa7ljTyksmkuaIXFYhgKHCACufqk_fPSbrCEP0JvyN-FfAClaY9ucK6EcZhu1E2aC8LkkNvrmPSSUgcX7hOG6TywZTvCspUpktgXUN-Qd2WFgFqKwlOLOSm4R_GLCJzxS4WIu98vDh3dT7c6x7KP5IQDLRznrATglQ_gPkqUtSE5fFcMSEO7OF0BbRqVXQeILMLfp8xgaFPl6zJI-UvrQ0lL93nIkjsIDsukd",
   "results" : [
      {
         "geometry" : {
            "location" : {
               "lat" : 30.73732,
               "lng" : 76.785848
            }
         },
         "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/jewelry-71.png",
         "id" : "9e3b67275d7c32b85c301d1ad708519ad11bd5e4",
         "name" : "Tanishq",
         "opening_hours" : {
            "open_now" : true,
            "weekday_text" : []
         },
         "place_id" : "ChIJx3CfWf9mUjoRbOxyCwd6Ujg",
         "reference" : "CmRaAAAAAHqTGUQISzXGLMXzxqDCn29rSuDc-vBCOczSdGVZwxBaNkVE2sFEoYio7HpH6KzAYkOgkQGKHb7tKVSk2_XYObCLmvIpLT57GFUQhr0l8uWNabXWjkco79L6kyEJyC_nEhCObWMxWqub2GZ0ZXanC6msGhRUdmwS-FFoNL-EMeyKDF1w-P0lrg",
         "scope" : "GOOGLE",
         "types" : [ "jewelry_store", "store", "establishment" ],
         "vicinity" : "Himalaya Marg, Sector 17, Chandigarh"
      },
      {
         "geometry" : {
            "location" : {
               "lat" : 30.734622,
               "lng" : 76.76966
            }
         },
         "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/jewelry-71.png",
         "id" : "fec37080b9573fd4c98dfa8effe0be741362451f",
         "name" : "Jain Jewellers",
         "opening_hours" : {
            "open_now" : true,
            "weekday_text" : []
         },
         "photos" : [
            {
               "height" : 475,
               "html_attributions" : [],
               "photo_reference" : "CpQBggAAAGCpQElDqh0LsnCZYa2IOP8JNW_a0_1-VgCHsF-HlZjPeUyWS05j99LwPnBrkp6-Iz9HLxEJ3MQtJjJ39fudLldi9HeyGYpmI5zspYP7cFORcBTQJbT-yqw2Wf1a3sT1d1ry3GLn0m443DsDZgTSbN-Kmq91eZ2bfR30g3VXyFqUpNPrSAbDHQxplC9qrfmvTRIQ3RQjvxNNtFh02cx3xlxsWhoUL5yDyapLNkWo9t3mNldvdsyBk1o",
               "width" : 475
            }
         ],
         "place_id" : "ChIJzaqqqqXtDzkRv6HcbBzPtyM",
         "reference" : "CnRhAAAAwbGT9XUseXle30IC6PRphi6ekxeQkL4s9K_S6M8NUJAZn-mI9LgdXW_muYuaDE34nDNpJcDUMX4jgYqsgreIBETH3aC1cZhu0GRHakYOSyWw5Z2TtflGwOtkNqaI1phZ1C14tIlVf2gN5FtWdN4XLBIQbGGCgCEi2wj5EC3H5BzbqRoUGQ4QJzKXOnXSMlSXMR5PokdY9JE",
         "scope" : "GOOGLE",
         "types" : [ "finance", "jewelry_store", "store", "establishment" ],
         "vicinity" : "SCF 8, SECTOR 22 D, Chandigarh"
      },

This is a part of json . I am getting problem on fetching opening_hours. It says the following item does not exist but it is coming in the K. Here is my json code :

JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
contacts = jsonObj.getJSONArray("results");

// looping through All Contacts
for (int i = 0; i < contacts.length(); i++) {
    JSONObject c = contacts.getJSONObject(i);

    String name = c.getString("name");
    names.add(name);
    Log.v("name is", name);
    // cat.add(catagory);
    String vicinity = c.getString("vicinity");
    Log.v("vicinity is", vicinity);
    location.add(vicinity);
    // link.add(img_link);

    String open = c.getString("opening_hours");

对于一些结果值是开放字符串,但在某一点,它说,价值不存在和代码停止那里。

这里是logcat:

05-04 18:33:01.451: W/System.err(3695): org.json.jsonexception:为opening_hours没有价值
05-04 18:33:01.461: W/System.err(3695):     at org.json.JSONObject.get(JSONObject.java:354)
05-04 18:33:01.461: W/System.err(3695):     at org.json.JSONObject.getString(JSONObject.java:510)
05-04 18:33:01.461: W/System.err(3695):     at com.app.jewel.HomeFragment$GetJewelry.doInBackground(HomeFragment.java:153)
05-04 18:33:01.461: W/System.err(3695):     at com.app.jewel.HomeFragment$GetJewelry.doInBackground(HomeFragment.java:1)
05-04 18:33:01.461: W/System.err(3695):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
05-04 18:33:01.461: W/System.err(3695):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
05-04 18:33:01.461: W/System.err(3695):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
05-04 18:33:01.471: W/System.err(3695):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
05-04 18:33:01.471: W/System.err(3695):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
05-04 18:33:01.471: W/System.err(3695):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
05-04 18:33:01.471: W/System.err(3695):     at java.lang.Thread.run(Thread.java:856)

请帮助

answer1: 回答1:

opening_hours isn't a string; it's another JSONObject:

JSONObject jsonObjectOpeningHours = c.getJSONObject("opening_hours");

opening_hours不是字符串;它的另一个JSONObject:

JSONObject jsonObjectOpeningHours = c.getJSONObject("opening_hours");
answer2: 回答2:

You have an error in this string of code.

String open = c.getString("opening_hours");

As I see in json object you are showing, opening_hours is not a String. It's a json object too. Try something like that

JSONObject open = c.getJSONObject("opening_hours");

此代码串中有错误。

String open = c.getString("opening_hours");

在你表现出的JSON对象看到我,opening_hours不是一个字符串。这是一个JSON对象太。尝试这样的东西

JSONObject open = c.getJSONObject("opening_hours");
answer3: 回答3:

It is because Google Places API sometimes does not return opening_hours tag. So even if you parse the JSON correctly it causes JSONException.

It is because Google Places API sometimes does not return opening_hours tag. So even if you parse the JSON correctly it causes JSONException.

android  json