我正在追踪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"
},
这是json的一部分。我在获取opening_hours
时遇到问题。它说下面的项目不存在但是它正在进入K.
这是我的json代码:
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: 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)
请帮忙
答案 0 :(得分:3)
opening_hours不是字符串;它是另一个JSONObject:
JSONObject jsonObjectOpeningHours = c.getJSONObject("opening_hours");
答案 1 :(得分:0)
您在此代码字符串中出错。
String open = c.getString("opening_hours");
正如我在json对象中看到的那样,opening_hours
不是String。它也是一个json对象。尝试类似的东西
JSONObject open = c.getJSONObject("opening_hours");
答案 2 :(得分:0)
这是因为Google Places API有时不会返回opening_hours
标记。
因此,即使您正确解析JSON,它也会导致JSONException
。