- Home /
converting excel to json: reading problem when file too large
I have a excel file which stored my game data, and successfully converted to json file.
A sample on excel file and the converted json:
The pretty output on the sample data:
While if the excel file contain a lot of data, which exceed the size of String can hold (I believe), problem on reading the json will exceed.
Hence I would like to ask how should my json file be wrote and read so I need not to care about the file( or string) size.
//function that create the json object ( using Simple Json library in eclipse)
public void createJson(String outputpath,XSSFSheet sheet){
XSSFRow singleRow;
XSSFCell cell;
int totalRows=getExcelTotalRow(sheet);
int totalCol=getExcelTotalColumn(sheet);
JSONObject jsonObject=new JSONObject();
String[] attributes=getJsonAttributes(sheet.getRow(0));
for(int i=1; i<totalRows;i++){
JSONObject jRow = new JSONObject();
singleRow=sheet.getRow(i);
String chipRoot="";
for(int j=0;j<totalCol;j++){
cell=singleRow.getCell(j);
cell.setCellType(Cell.CELL_TYPE_STRING);
jRow.put(attributes[j], cell.toString());
if(j==0){
chipRoot=cell.toString();
}
}
jsonObject.put(chipRoot, jRow);
System.out.print("\n");
}
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String jsonOutput = gson.toJson(jsonObject);
System.out.println(jsonOutput);
writeJsonFile(outputpath,jsonObject);
}
//and function to create the json file
public void writeJsonFile(String fileName, JSONObject jsonObject){
try {
//System.out.println(jsonObject);
fileName=fileName.replace(".txt", "2.txt");
FileWriter fw= new FileWriter(fileName);
BufferedWriter bw = new BufferedWriter(fw);
jsonObject.writeJSONString(fw);
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
The code for reading json in unity
public class chipManager :MonoBehaviour {
public string fileName;
public string chipJson;
public Dictionary<int, Chip> chipDictionary=new Dictionary<int, Chip>();
void Start(){
getChipData ();
putAllChipToDictionary ();
getChipByID (1);
getChipByID (2);
getChipByID (3);
getChipByID (5);
}
public void getChipData(){
try
{
string fileFullPath= Application.dataPath+"/textFiles/"+fileName;
StreamReader theReader = new StreamReader(fileFullPath, Encoding.Default);
chipJson=theReader.ReadToEnd();
theReader.Close();
}
catch (Exception e)
{
Debug.Log(e);
}
}
public void putAllChipToDictionary(){
var N = JSON.Parse (chipJson);
//Debug.Log (N.Count);
for(int i=0;i<N.Count;i++){
int id = N[i]["ChipID"].AsInt;
string chipName = N[i]["Name"];
int power = N[i]["Power"].AsInt;
string desc = N[i]["Desc"];
Chip tempChip= new Chip(id,chipName,power,desc);
chipDictionary.Add(id, tempChip);
}
}
public void getChipByID(int chipID){
Debug.Log ("getting chip "+ chipID);
Chip chipReturn= chipDictionary[chipID];
Debug.Log (chipReturn.ToString ());
}
}
Why do you believe that the data is too large to read? What exactly is the problem you're running into? If we don't know if it's errors, missing data, wrong behaviour or your children rejecting your advice as your generations are simply too out of touch, we cannot help you.
Your answer
Follow this Question
Related Questions
Parsing JsonData throws error when value is null, what to do? 0 Answers
JSON-Serializer (Renewed open) 0 Answers
Whew, I'm stuffed! (Hunger Script) 1 Answer
C# - Read JSON 1 Answer
Application.ExternalCall Issue 0 Answers