1

The json looks like this :

"hour_totals": {
      "382423": {
        "imp": 126,
        "clk": 1,
        "spend": "$0.03",
        "conv": 0,
        "cpm": "$0.22",
        "cpc": "$0.03",
        "ctr": "0.79%",
        "cpa": "$Inf"
      },
      "382424": {
        "imp": 209,
        "clk": 1,
        "spend": "$0.05",
        "conv": 0,
        "cpm": "$0.23",
        "cpc": "$0.05",
        "ctr": "0.48%",
        "cpa": "$Inf"
      }}

I read more than 20 answers, but unable to find how to deserialize such a structure, please help on what would the class look like since the hour is not a fixed string.

4
  • I don't think a JSON to Java Object library would work with JSON like this automatically. What it looks like you want is to store the hour nodes as a Map<String,HourDetailObject> but to do this I think you are just going to have to iterate over each node kind of like how people used to read XML Commented Oct 5, 2013 at 18:41
  • @JasonSperske thanks, I just thought the same and it worked. FYI google-gson was able to handle this type of json. Commented Oct 5, 2013 at 19:00
  • @coding you should add your final code as an answer to your own question so people can vote it up. Commented Oct 5, 2013 at 21:25
  • @JasonSperske: Gson can parse it assuming that string posted in question is transformed into a valid JSON. Commented Oct 5, 2013 at 22:00

1 Answer 1

1

To parse this JSON with Gson you need two steps.

  1. Define this classes:

    public class Total {
    
       Map<String, HourData> hour_totals;
    
       @Override
       public String toString() {
          return "Total [hour_totals=" + hour_totals + "]";
       }
    
    }
    

    where HourData is

    public class HourData {
    
       Integer imp;
       Integer clk;
       String spend;
       Integer conv;
       String cpm;
       String cpc;
       String cpa;
    
       @Override
       public String toString() {
          return "HourData [imp=" + imp + ", clk=" + clk + ", spend=" + spend
                + ", conv=" + conv + ", cpm=" + cpm + ", cpc=" + cpc + ", cpa="
                + cpa + "]";
       }
    
     }
    
  2. Hack a bit your "Json string" since it's not a valid Json (see more details below). You just need to add braces like this code:

    public class Q19201300 {
    
      public static void main(String[] args) {
          String json = "\"hour_totals\": {  "
                  + "  \"382423\": {                 "
                  + "  \"imp\": 126,                 "
                  + "  \"clk\": 1,                   "
                  + "  \"spend\": \"$0.03\",         "
                  + "  \"conv\": 0,                  "
                  + "  \"cpm\": \"$0.22\",           "
                  + "  \"cpc\": \"$0.03\",           "
                  + "  \"ctr\": \"0.79%\",           "
                  + "  \"cpa\": \"$Inf\"             "
                  + "},                              "
                  + "\"382424\": {                   "
                  + "  \"imp\": 209,                 "
                  + "  \"clk\": 1,                   "
                  + "  \"spend\": \"$0.05\",         "
                  + "  \"conv\": 0,                  "
                  + "  \"cpm\": \"$0.23\",           "
                  + "  \"cpc\": \"$0.05\",           "
                  + "  \"ctr\": \"0.48%\",           "
                  + "  \"cpa\": \"$Inf\"             "
                  + "}}                              ";
    
          Total t = new Gson().fromJson("{" + json + "}", Total.class);
    
          System.out.println(t);
    
      }
    }
    

This will give you:

Total [hour_totals={382423=HourData [imp=126, clk=1, spend=$0.03, conv=0, cpm=$0.22, cpc=$0.03, cpa=$Inf], 382424=HourData [imp=209, clk=1, spend=$0.05, conv=0, cpm=$0.23, cpc=$0.05, cpa=$Inf]}]

About your string. From JSON official grammar (http://www.ietf.org/rfc/rfc4627.txt):

  1. JSON Grammar

    A JSON text is a sequence of tokens. The set of tokens includes six structural characters, strings, numbers, and three literal names.

    A JSON text is a serialized object or array.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.