I want to convert this JSON into objects in java:
{
"mapping": [
{
"boardPosition": {
"row": 1,
"col": 1
},
"nodeId": 3242324
},
{
"boardPosition": {
"row": 1,
"col": 2
},
"nodeId": 432423
},
{
"boardPosition": {
"row": 1,
"col": 3
},
"nodeId": 424324132
}
]
}
this is how I created my java classes
class MapeoWumpus {
public mapp mapping;
}
class mapp{
public boardP boardPosition;
public String nodeId;
}
class boardP{
public int row;
public int col;
}
and then when I try to convert my file like this
MapeoWumpus mapa=new MapeoWumpus();
mapa=gson.fromJson(filetext, MapeoWumpus.class);
I get a null object
What can I do?
EDIT: This is my entire code:
package parserjson;
import java.io.FileNotFoundException;
import java.util.*;
import com.google.gson.*;
public class Main {
/**
* @param args
*/
public static void main(String[] args) throws FileNotFoundException {
String filetext;
ParserJson parser=new ParserJson();
Gson gson=new Gson();
MapeoWumpus mapa=new MapeoWumpus();
filetext=parser.leerArchivo("b1.json");
mapa=gson.fromJson(filetext, MapeoWumpus.class);
}
}
“leerArchivo” is just a method to get the json file, as you can see my json file is in a string variable
You should define instance variable
mappas array. Because your JSON data seems to contain mapping array.Creating new MapeoWumpus in the below code is unnecessary
Just change it as follows