2012-02-14 2 views
0
{"1": { 
     "StoreName": "イオン八千代緑が丘店", 
     "StoreTel": "047-480-3660", 
     "StoreAddress": "〒276-0049 千葉県八千代市緑ヶ丘2-1-3 2F", 
     "WorkingTimeInNormalDay": "7:30 AM - 9:00PM", 
     "WorkingTimeInWeekend": "9:00-22:00", 
     "HaveKidProduct": "N" 
    }} 

je veux lire JSON:JSONArray avec caractère ":" dans Android

JSONObject json ,jChild; 
JSONArray jsonNames, jsonValues; 
JSONArray jChildNames,jChildValues; 
json = new JSONObject(resultString); 

jsonNames = json.names(); 
jsonValues = json.toJSONArray(jsonNames); 
for (int i = 0; i < jsonNames.length(); i++) { 
jChild = jsonValues.getJSONObject(i); 
jChildNames = jChild.names(); 
jChildValues = jChild.toJSONArray(jChildNames); 

Log.i(getCallingPackage(), "No : " + jsonNames.getString(i)); 
       for (int j = 0; j < jChildNames.length(); j++){ 
        Log.i(getCallingPackage(),jChildNames.getString(j) + " : " + jChildValues.getString(j).trim()); 

       } 

} 
} 

-il des problèmes ici est: Quand je parse ceci:

"WorkingTimeInNormalDay": « 7: 30 h - 21h00 », j'ai

jChildValues.getString (j) retour "7" et non "07h30-21h00"

je pense peut être ":" caractère est la cause racine

Comment puis-je résoudre ce problème?

Merci

===========

Modifié:

Ceci est mon erreur

tout fonctionne comme un champion

+1

Non, ' « 07h30-21h00 »' ne devrait pas être à l'origine de problème comme enfermé dans des guillemets doubles – waqaslam

+0

mais si je n'ai pas « WorkingTimeInNormalDay »: "7:30 AM - 21:00", "WorkingTimeInWeekend": "9: 00-22: 00", tout est correct –

+0

Je pense que c'est un problème d'encodage. Imprimez votre chaîne JSON. – Fredrik

Répondre

0

Oh, il n'y a aucun problème avec caractère « : ».

Il y a une grosse erreur.

Tout est travail bien

Désolé

1

Avant l'analyse de la json essaie d'imprimer la chaîne json totale pour que vous sachiez que "WorkingTimeInNormalDay": "7:30 AM - 21:00" arrive correctement ou non. Parce que si vous analysez le JSON le WorkingTimeInNormalDay vous donnera 07h30-21h00

+0

"StoreName": "," "StoreTel": "047-480-3660", "StoreAddress": "〒276-0049 千葉 県 代 市 緑 緑 ヶ 丘 2-1-3 2F", je peux analyser correctement tous les –

+0

WorkingTimeInNormalDay est imprimé correctement ??? – Vamshi

+1

Oui, l'exemple JSON semble être correctement formaté (en ajoutant le {} requis requis pour en faire un objet JSON) - il semble donc que votre entrée réelle est invalide. Si vous obtenez le JSON d'une source externe disponible, vous devriez envisager de l'exécuter via un [validateur en ligne, comme celui-ci] (http://jsonformatter.curiousconcept.com/). – Jens

1

bien il oeuvra

String json = "{\"1\": [{\"StoreName\": \"イオン八千代緑が丘店\",\"StoreTel\": \"047-480-   3660\"," + 
       "\"StoreAddress\": \"〒276-0049 千葉県八千代市緑ヶ丘2-1-3 2F\"," + 
       "\"WorkingTimeInNormalDay\": \"7:30 AM - 9:00PM\"," + 
       "\"WorkingTimeInWeekend\": \"9:00-22:00\"," + 
       "\"HaveKidProduct\": \"N\"" + 
       "}]}"; 


      try { 
       JSONObject e = new JSONObject(json); 
       JSONArray jArray = e.getJSONArray("1"); 
       for(int i=0;i<jArray.length();i++){ 
       JSONObject obj = jArray.getJSONObject(i); 
       System.out.println(obj.getString("WorkingTimeInNormalDay")); 
       } 

       System.out.println(jArray.getString(0)); 
      } catch (JSONException e) { 
       Log.e("log_tag", "Error parsing data " + e.toString()); 
      } 
+0

comment puis-je ajouter \ automaticicaly –

+0

je n'ai pas échappé si vous regardez plus attentivement. Je n'ai pas échappé au: signe. J'ai échappé aux doubles guillemets. –

1

Jetez un oeil à la bibliothèque Google GSON pour Android. Vous pouvez facilement ajouter vos propres analyseurs pour des types de données spécifiques.

Par exemple:

public class DateDeserializer implements JsonDeserializer<Date> 
{ 
    public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException 
    { 
     String JSONDateToMilliseconds = "\\/(Date\\((.*?)(\\+.*)?\\))\\/"; 
     Pattern pattern = Pattern.compile(JSONDateToMilliseconds); 
     Matcher matcher = pattern.matcher(json.getAsJsonPrimitive().getAsString()); 
     String result = matcher.replaceAll("$2"); 

     return new Date(new Long(result)); 
    } 
} 
+0

mais cela vous fera empaqueter cette bibliothèque avec votre apk au lieu de simplement utiliser le natif. –