2010-11-15 11 views
1

Ceci est mon code:JSONException: objet Misplaced

JSONStringer result = new JSONStringer(); 

    for (long i = start; i <= end; i = i + day) { 
     ttm.put("$gte", "" + i); 
     ttm.put("$lte", "" + (i + day)); 
     //code code code 

     int count = statisticCollection.find(query).count(); 

     try { 
      result.object().key("ttm").value(i).key("count").value(count); 
     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

    try { 
     result.endObject(); 
    } catch (JSONException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

je puis obtenir un JSONException. J'ai aussi essayé de créer et de mettre fin à l'objet avec un bloc try-catch différent, comme ci-dessous:

JSONStringer result = new JSONStringer(); 

    try { 
     result.object(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    for (long i = start; i <= end; i = i + day) { 
     ttm.put("$gte", "" + i); 
     ttm.put("$lte", "" + (i + day)); 

     //code code code 

     long count = statisticCollection.find(query).count(); 

     try { 
      result.key("ttm").value(i).key("count").value(count); 
     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

    try { 
     result.endObject(); 
    } catch (JSONException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

et la création et la fin de la JSONStringer dans la boucle elle-même, comme suit:

JSONStringer result = new JSONStringer(); 

for (long i = start; i <= end; i = i + day) { 
    ttm.put("$gte", "" + i); 
    ttm.put("$lte", "" + (i + day)); 
    //code code code 

    int count = statisticCollection.find(query).count(); 

try { 
    result.object().key("ttm").value(i).key("count").value(count).endObject(); 
} catch (JSONException e) { 
      e.printStackTrace(); 
    } 

Qu'est-ce que Je fais mal?

Merci.

+0

Le message d'exception (et éventuellement le numéro de ligne), espérons-être utile pour travailler ce que le problème est. À l'heure actuelle, cela donne plus de contexte que «quelque chose s'est mal passé» (ce qui était déjà clair dans votre question). –

+0

La trace est collée ici: http://pastebin.ca/1991983 – theTuxRacer

Répondre

1

Vous devez utiliser un tableau:

JSONStringer result = new JSONStringer(); 
JSONWriter array = result.array(); 

for (long i = start; i <= end; i = i + day) { 
    ttm.put("$gte", "" + i); 
    ttm.put("$lte", "" + (i + day)); 
    //code code code 

    int count = statisticCollection.find(query).count(); 

    try { 
     array.object().key("ttm").value(i).key("count").value(count).endObject(); 
    } catch (JSONException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 

try { 
    array.endArray(); 
} catch (JSONException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 
+0

wow, ça a marché! pourriez-vous me dire ce que je faisais mal? Je vois que vous avez ajouté un JSONWriter. – theTuxRacer