0

For example my JSON text is coming like this.

"pages":{"42010":{"pageid":42010,"ns":0,"title":"Queen (band)"}}

Because everytime my json text is coming with different number which is inside pages tag.

How do i convert this to Java equivalent class?

Currently my generated java class is something like this.

@Generated("org.jsonschema2pojo")
public class Pages {

    @SerializedName("42010")
    @Expose
    private _42010 _42010;
}

That _42010 class contains the inner fields like "pageid":42010,"ns":0,"title":"Queen (band)", since i am getting everytime new number inside pages, its not working. its working only for the specific json text.

3 Answers 3

1

You can use a custom deserialiser that ignored the changing number. For example:

package jacksonTest;

import java.io.IOException;
import java.lang.reflect.Type;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

public class CustomDeserialiser {


public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
    String json = "{\"42010\":{\"pageid\":42010,\"ns\":0,\"title\":\"Queen (band)\"}}";
    String json2 = "{\"12345\":{\"pageid\":12345,\"ns\":0,\"title\":\"Queen (band)\"}}";

    Gson g = new GsonBuilder().registerTypeAdapter(Pages.class, new PagesDeserialiser()).create(); 

    Pages fromJson = g.fromJson(json, Pages.class);
    System.out.println(fromJson);

    fromJson = g.fromJson(json2, Pages.class);
    System.out.println(fromJson);
}

public static class PagesDeserialiser implements JsonDeserializer<Pages> {

    @Override
    public Pages deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
            throws com.google.gson.JsonParseException {
        JsonObject object = json.getAsJsonObject();

        Pages p = new Pages();
        object.entrySet().forEach( e -> {
            JsonObject tmp = e.getValue().getAsJsonObject();
            if(tmp.get("pageid") != null) {
                // right object
                p._42010 = new _42010();
                p._42010.ns = tmp.get("ns").getAsInt();
                p._42010.pageid = tmp.get("pageid").getAsInt();
                p._42010.title = tmp.get("title").getAsString();
            }
        });

        return p;
    }

}

public static class Pages {

    _42010 _42010;

    @Override
    public String toString() {
        return _42010.toString();
    }


}

public static class  _42010 {
    int pageid;
    int ns;
    String title;

    @Override
    public String toString() {
        return title + " " + pageid + " " + ns;
    }
}

}

The deserialiser for type pages simply checks the entries to find the one that contains a pageId and then populates the class.

Running my test gives you:

Queen (band) 42010 0
Queen (band) 12345 0

I am assuming that you are using Gson as your json library.

Regards,

Artur

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

2 Comments

Could you please tell me what is the Type Class used in Deserializer? which library is it from?
This is the Gson library. I believe that is the same as yours? I will add my imports to the snippet
0

Why do not use an JSON library like jackson or org.json?

Make your json correct like

{
"pages":{
       "42010":{
               "pageid":42010,
               "ns":0,
               "title":"Queen (band)"
               }
        }
}

And you will be able to use it like :

JSONObject jsonObjet = new JSONObject(yourJson);
jsonObjet.get("pages");

2 Comments

@Gregoire PORTIER: everytime i am getting the new int value inside pages[42010], how do i get the title directly.
Check this thread : stackoverflow.com/questions/20899839/… It's simple, and very usefull to learn how to use it. If you want to use Json with Java, imo org.json is the easier way to do it.
0

Ideally it should be using Map.

This helps in forming the values as Map<Integer, Pojo>.

Lets say

public class Pojo{
    private int pageid;
    private String title;
    private int ns;
    // getter and setter
}

This suffices the requirement of holding the random digits, generated at runtime,

1 Comment

Since i am using GSON library to convert json to java, the above code is not working.

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.