我有下面的项目,如何将嵌套的对象解析为java变量。我正在使用JSON文档,但不确定如何解析子对象。请参阅JSON项目和Java代码
JSON:
{
"_id" : "328",
"website" : "www.abc.com",
"addresses" : [
{
"addressType" : "PRIMARY",
"address" : "102 Yonge Street",
"city" : "Toronto",
"postalCode" : "M2N 6T6",
"state" : {
"code" : "ON",
"name" : "Ontario",
"countryCode" : "CA"
},
"country" : "Canada"
},
{
"addressType" : "SECONDARY",
"address" : "180 Yonge Street",
"city" : "Toronto",
"postalCode" : "L6P 6T7",
"state" : {
"code" : "ON",
"name" : "Ontario",
"countryCode" : "CA"
},
"country" : "Canada"
}
],
"phoneNumber" : "123456"
}
Java代码:
DynamicMappingFile objDynoMapping = new DynamicMappingFile();
ConfigReader objConfigReader = new ConfigReader();
public String strSQL(String strFullDocument)
{
String sqlInsertStatement = "";
System.out.println("Value of strFullDocument is " + strFullDocument);
String lstrInsertColumnStatement ="";
String charAfterColumnStatement = ")";
String lstrInsertValuesStatement ="values (";
String lstrColumnValues = "";
String lstrBSONDocValue = "";
String lstrColumnName;
int columnIndex = 0;
int documentLength = BsonDocument.parse(strFullDocument).size();
final BufferedReader br;
String lstrMySQLColumneName="";
String MySQLColumnNames = "";
String lstrMongoandMySQLDBFields="";
int lstrBSONDoubleValue = 0;
try {
br = new BufferedReader(new FileReader("./Configuration/MongoMySQLMapping.csv"));
lstrInsertColumnStatement = lstrInsertColumnStatement.concat("Insert into " + objConfigReader.mySQLTable + " (");
System.out.println("Value of Insert statement is : " + lstrInsertColumnStatement);
while(br.ready()){
lstrMongoandMySQLDBFields = br.readLine(); //Skip the header line
System.out.println(lstrMongoandMySQLDBFields);
objDynoMapping.add(objDynoMapping.new MapFields(lstrMongoandMySQLDBFields));
}
}
catch(Exception errBuff) {
System.out.println(errBuff.toString());
}
System.out.println("Length of Document is " + documentLength);
lstrColumnName = BsonDocument.parse(strFullDocument).keySet().toString().replace("[", "").replace("]", "");
System.out.println("value of lstrcolumnname");
Set<String> setKeys = BsonDocument.parse(strFullDocument).keySet();
Iterator<String> itr = setKeys.iterator();
System.out.println("Value of keys");
while(itr.hasNext())
{
System.out.println("Inside while");
String keys = (String) itr.next();
lstrMySQLColumneName = objDynoMapping.lsMapFields.get(keys).keySet().toString().replace("[", "").replace("]", "");
System.out.println("lstrMySQLColumneName" + lstrMySQLColumneName);
if(BsonDocument.parse(strFullDocument).get(keys).isObjectId())
{
System.out.println("Inside object id");
lstrBSONDocValue = (String)BsonDocument.parse(strFullDocument).get(keys).asObjectId().getValue().toString();
System.out.println("lstrBSONDocValue is" + lstrBSONDocValue);
}
else if(BsonDocument.parse(strFullDocument).get(keys).isString())
{
// details[i] = BsonDocument.parse(strFullDocument).get(keys).asString().getValue();
System.out.println("Inside else of string");
lstrBSONDocValue = (String)BsonDocument.parse(strFullDocument).get(keys).asString().getValue();
System.out.println("Value is " + lstrColumnValues);
// System.out.println("Value of details is " + details[i]);
}
else if(BsonDocument.parse(strFullDocument).get(keys).isInt32())
{
// details[i] = BsonDocument.parse(strFullDocument).get(keys).asString().getValue();
System.out.println("Inside int value");
lstrBSONDocValue = String.valueOf(BsonDocument.parse(strFullDocument).get(keys).asInt32().getValue());
System.out.println("Value is " + lstrBSONDoubleValue);
// System.out.println("Value of details is " + details[i]);
}
if(columnIndex!=documentLength-1)
{
System.out.println("insode column index");
MySQLColumnNames = MySQLColumnNames.concat(lstrMySQLColumneName + ",");
if(BsonDocument.parse(strFullDocument).get(keys).isObjectId())
{
System.out.println("Before is " + lstrColumnValues);
lstrColumnValues = lstrColumnValues.concat("\"" + lstrBSONDocValue + "\",");
System.out.println("After is " + lstrColumnValues);
}
if(BsonDocument.parse(strFullDocument).get(keys).isString())
{
System.out.println("Before is " + lstrColumnValues);
lstrColumnValues = lstrColumnValues.concat("\"" + lstrBSONDocValue + "\",");
System.out.println("After is " + lstrColumnValues);
}
if(BsonDocument.parse(strFullDocument).get(keys).isInt32())
{
System.out.println("Before is " + lstrColumnValues);
lstrColumnValues = lstrColumnValues.concat(String.valueOf(lstrBSONDocValue) + ",");
System.out.println("After is " + lstrColumnValues);
}
}
else
{
System.out.println("Insode else column");
MySQLColumnNames = MySQLColumnNames.concat(lstrMySQLColumneName);
if(BsonDocument.parse(strFullDocument).get(keys).isObjectId())
{
System.out.println("Before is " + lstrColumnValues);
lstrColumnValues = lstrColumnValues.concat("\"" + lstrBSONDocValue + "\",");
System.out.println("After is " + lstrColumnValues);
}
if(BsonDocument.parse(strFullDocument).get(keys).isString())
{
System.out.println("Before is " + lstrColumnValues);
lstrColumnValues = lstrColumnValues.concat("\"" + lstrBSONDocValue + "\"");
System.out.println("After is " + lstrColumnValues);
}
if(BsonDocument.parse(strFullDocument).get(keys).isInt32())
{
System.out.println("Before is " + lstrColumnValues);
lstrColumnValues = lstrColumnValues.concat(String.valueOf(lstrBSONDocValue));
System.out.println("After is " + lstrColumnValues);
}
//
// System.out.println("lstrColumnValues 1 is " + lstrColumnValues);
}
columnIndex++;
}
sqlInsertStatement = sqlInsertStatement.concat(lstrInsertColumnStatement + MySQLColumnNames + charAfterColumnStatement + lstrInsertValuesStatement + lstrColumnValues + ")");
System.out.println("Final Value of Insert is " + sqlInsertStatement);
return sqlInsertStatement;
}