Question

I have JSON like this:

{
    "flux":{
        "1400364000":{
            "Optaf_matchdata":{
                "uid":"749674", "fk_comp_id":"2414", "comp_id":"112", "saisonid":"2013", "temps":null, "matchday":"40"
            }
        },
        "1400104800":{
            "Optaf_matchdata":{
                "uid":"749670", "fk_comp_id":"2414", "comp_id":"112", "saisonid":"2013", "temps":null, "matchday":"39"
            }
        }
    }
}

The problem is that the keys 1400364000 and 1400104800 are variable. How can I put this in a @JsonProperty, when I don't have the name?

I can retrieve those key 1400364000, 1400104800 separately. How can I retrieve the Optaf_matchdata from the 1400364000 key, for example?

Was it helpful?

Solution

I resolve this by add a customer Deserialize like this:

 @JsonIgnoreProperties(ignoreUnknown = true)
public class CalendarResultsDto {
@JsonDeserialize(using=JsonMatchDeserialize.class)
@JsonProperty(value="flux")

So after that a return a Map witch String it's the key. JsonMatchDeserialize.java:

public class JsonMatchDeserialize extends JsonDeserializer> {

@Override
public Map<String, CalendarMatchDataDto> deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
        throws IOException, JsonProcessingException {

    Map<String, CalendarMatchDataDto> mapToReturn = new  HashMap<String, CalendarMatchDataDto>();
    JSONObject flux;
    try {
        flux = new JSONObject(jsonParser.getValueAsString());
        ObjectMapper mapper = new ObjectMapper();
         Iterator<String> iter = flux.keys();
            while (iter.hasNext()) {
                String key = iter.next();
                try {
                    Object value = flux.get(key);
                    CalendarMatchDataDto calendarMatchDataDto =  mapper.readValue(value.toString(), CalendarMatchDataDto.class); 
                    if(key!=null && calendarMatchDataDto!=null)
                        mapToReturn.put(key, calendarMatchDataDto);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
    } catch (JSONException e1) {
        e1.printStackTrace();
    }




    return mapToReturn;



}

and CalendarMatchDataDto.java

    public class CalendarMatchDataDto {

@JsonProperty(value="Optaf_matchdata")
public MatchDto matchDto ;
       }

OTHER TIPS

UPDATE: I just realized you were using Jackson to automagically convert your JSON, instead of parsing it manually as I suggested. My previous answer might not suit your needs at all.

My remark would then be: what are those variable keys? They should probably be a value for a key named "id" or something.

Also, you should consider using a JSON array.

Here is an example of what your JSON should probably look like instead:

{
   "flux":[
      {
         "id":"1400364000",
         "Optaf_matchdata":{
            "uid":"749674", "fk_comp_id":"2414", "comp_id":"112", "saisonid":"2013", "temps":null, "matchday":"40"
         }
      },
      {
         "id":"1400104800",
         "Optaf_matchdata":{
            "uid":"749670", "fk_comp_id":"2414", "comp_id":"112", "saisonid":"2013", "temps":null, "matchday":"39"
         }
      }
   ]
}

ORIGINAL ANSWER

how can i get their Optaf_matchdata from this key 1400364000 for example

Not sure I understood your question, but here is my attempt to answer.

You can use the getJSONObject() method:

JSONObject source = new JSONObject(jsonString);
JSONObject flux = source.getJSONObject("flux");
JSONObject item = flux.getJSONObject("1400364000"); // this string can be dynamic
JSONObject optafMatchData = item.getJSONObject("Optaf_matchdata");

You could even iterate on all keys of the flux object (the 1400364000-like keys):

JSONObject source = new JSONObject(jsonString);
JSONObject flux = source.getJSONObject("flux");
Iterator<?> keys = flux.keys();
while(keys.hasNext()){
    String key = (String) keys.next();
    JSONObject item = flux.getJSONObject(key);
    JSONObject optafMatchData = item.getJSONObject("Optaf_matchdata");
    // do whatever with this item/match data
}

Then you might want to write a method that would parse an OptafMatchData object from the JSON:

private static OptafMatchData parseMatchData(JSONObject item) throws JSONException {
    OptafMatchData omd = new OptafMatchData();
    omd.setUid(item.getLong("uid"));
    omd.setFkCompId(item.getLong("fk_comp_id"));
    omd.setCompId(item.getLong("comp_id"));
    // and so on...
    return omd;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top