2013-01-28 2 views
3

Parfois j'ai obtenu différent tag chaîne dans tableau JSON par exempleParfois, j'ai obtenu différentes chaîne Clé: Valeur en JSON Tableau

{ 
"html_attributions": [], 
"results": [ 
    { 

     "name": "V & P FOX LOCKSMITHS", 
     "opening_hours": { 
      "open_now": true 
     }, 
     "vicinity": "91A Saint Martin's Lane, London" 
    }, 
    { 
     "id": "c680cf44d85a15cdc727e0101f8531db70c0cf1c",    
      "name": "London United Kingdom", 
     "vicinity": "41-43 Wardour Street, London" 

     // Here i do not get open hours tag, what should do when we 
     //not get json parsing in synchrozied manner 

    }, 
    { 

     "name": "Timpson", 
     "opening_hours": { 
      "open_now": true 
     }, 
     "vicinity": "40 Villiers Street, Charing Cross" 
    }] } 

résultat Parsing dépendent de l'emplacement où je un emplacement particulier au Royaume-Uni. J'ai trois attribut dans l'analyse. Nom, ville, heures d'ouverture.

Mais quand changé l'emplacement du Royaume-Uni aux Etats-Unis, j'ai seulement deux attribut comme Nom, ville. Pas eu d'heures d'ouverture, mais j'ai utilisé trois variables dans mon code pour tous les endroits. Mais quand nous sommes arrivés seulement deux valeurs de service Web, je suis erreur dans l'analyse syntaxique

Mon code Java est

JSONObject json = jParser.getJSONFromUrl(url); 
try { 
    // Getting Array of Contacts 
    towLogSmithJsonArray = json.getJSONArray(TAG_RESULTS_LOG_SMITH); 
    if(towLogSmithJsonArray!=null) 
    { 
      // looping through All Contacts 
     for(int i = 0; i < towLogSmithJsonArray.length(); i++) 
       { 
        JSONObject d = towLogSmithJsonArray.getJSONObject(i); 
        // Storing each json item in variable 
        String openNOW=""; 

        String nameCarRental = d.getString(TAG_NAME_LOG_SMITH);    
      String cityAddress = d.getString(TAG_CITY_LOCATION_LOG); 

      // JSONObject phone = d.getJSONObject(TAG_OPENING_HOURS); 
      // openNOW = phone.getString(TAG_OPEN_NOW);   

      Log.e("nameCarREntal", nameCarRental); 
      Log.e("CityAddress",cityAddress); 
      //Log.e("OPenNow",openNOW); 

      // creating new HashMap 
      HashMap<String, String> mapLockSmith = new HashMap<String, String>();    
      // adding each child node to HashMap key => value 

      mapLockSmith.put(TAG_NAME_LOG_SMITH, nameCarRental); 
      mapLockSmith.put(TAG_OPEN_NOW, openNOW); 
      mapLockSmith.put(TAG_CITY_LOCATION_LOG, cityAddress); 

      // adding HashList to ArrayList 
      towLogSmithList.add(mapLockSmith); 
      } 
      } 
     else 
       { 
      Log.d("LOck Smith parsing NUll: ", "null"); 
      Toast.makeText(LockSmith.this,"There is not data for particular location",Toast.LENGTH_LONG).show(); 
     }  
     }catch (JSONException e) {  
    e.printStackTrace(); 
} 

Quels changements devraient être là, quand je suis arrivé rien pour Tag « openNOw »

trois façons

-OpenNow: True 
-OPenNow: False 
-“opening_hours” Tag is not available in Pasring array code. 

Répondre

2

vérifier si le "opening_hours" un e "open_now" est présenté dans votre objet:

String openNOW = ""; 
if(d.has("opening_hours"){ 
    JSONObject json2 = d.getJSONObject("opening_hours"); 
    if(json2.has("open_now"){ 
     openNOW=json2.getString("open_now"); 
    } 
} 
+0

Salut, je question, puis-je utiliser chaîne openNOW; \t \t \t \t \t \t \t if (phone.equals ("")) \t \t \t \t \t \t \t { \t \t \t \t \t \t \t \t openNOW = "NULL"; \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t Log.e ("Si HEURES D'OUVERTURE", openNOW); \t \t \t \t \t \t \t} \t \t \t \t \t \t \t autre \t \t \t \t \t \t \t { \t \t \t \t \t \t \t openNOW = phone.getString (TAG_OPEN_NOW_LOG); \t \t \t \t \t \t \t \t \t \t \t \t \t \t Log.e ("AUTRES HEURES D'OUVERTURE", openNOW); \t \t \t \t \t \t \t} Dans propre projet, mais comment je peux sauver la chaîne itérer dans la chaîne comme ci-dessus donnée dans ma question. s'il vous plaît aider Merci – Rank

+0

Je ne comprends pas ..? Qu'essayez-vous d'accomplir? – mihail

+0

Je n'ai pas accès à cette URL. Vous devriez vérifier si l'élément existe dans l'objet actuel. c'est la méthode 'has'. Vérifiez la réponse. – mihail

Questions connexes