JSON作为KEY有任何保留字吗?
我的Json结构是
dimObject{String:String}
finalObject(String:dimObject}
Line1# JSONObject dimObject=new JSONObject()
Line2# dimObject.put("class",["A","B","c"]);
Line3# dimObject.put("name",["sam"]);
Line4# System.out.print("dimObject#"+dimObject.toString());
Line5# JSONObject finalObject=new new JSONObect();
Line6# finalObject("supplier",dimObject);
Line7# System.out.print("finalObject#"+finalObject.toString());
Run Code Online (Sandbox Code Playgroud)
OUTPUT:
dimObject#{"class":["A","B","c"],"name":["sam"]}
finalObject#{"supplier":{"name":["sam"]}}
Run Code Online (Sandbox Code Playgroud)
所以问题是为什么我的json表现得很奇怪.
我没有将"class"定义为变量名,它只是一个Key.
问题是,如果给出一个参数,"class"是一个键或保留字,那么我是如何成功插入的Line#2,如果它的插入能够在dimObject中那么为什么它不能在finalObject中插入.
请帮我解开这个谜
精确代码::
public JSONObject getRequiredAttributesWithValues() {
List<UserConstraint> constraintList = new ArrayList<UserConstraint>();
Map<String, FactTableOptimizingInfo> factTableMap = MappingInfo.INSTANCE.
getFactTableUserNameToFactTableOptimizingInfoMap();
Map<String, DimensionTableInfo> dimTableMap = MappingInfo.INSTANCE.getDimTableRealNameToObjectMap();
JSONObject requiredAttributes = getRequiredAttributes();
JSONObject finalObject = new JSONObject();
for (Object dimName : requiredAttributes.keySet()) {
JSONObject dimObject = new JSONObject();
JSONArray colNames = requiredAttributes.getJSONArray((String) dimName);
for (Object colName : colNames) {
List<String> columnList = new ArrayList<String>();
String dimensionName = (String) dimName;
String columnName = (String) colName;
constraintList = new ArrayList<UserConstraint>();
for (FilterDataStructure filter : this.info.getGlobalFilterList()) {
if (filter.getDimName().equals(dimensionName)) {
if (filter.getColumnName().equals(columnName)) {
AtomicConstraint.ConstraintType type;
try {
Integer.parseInt(filter.getValue());
type = AtomicConstraint.ConstraintType.INTEGER_TYPE;
} catch (NumberFormatException e) {
type = AtomicConstraint.ConstraintType.STRING_TYPE;
}
UserConstraint constraint = new UserAtomicConstraint(dimensionName, columnName,
AtomicConstraint.getStringToOperator(filter.getOperator()),
filter.getValue(), type, factTableMap, dimTableMap);
constraintList.add(constraint);
}
}
}
columnList.add(columnName);
List<UserDimensionInfoToBuildQuery> dimList = new ArrayList<UserDimensionInfoToBuildQuery>();
UserTableAndColInfo groupByInfo = new UserTableAndColInfo(dimensionName, columnName);
ArrayList<UserTableAndColInfo> groupByInfoList = new ArrayList<UserTableAndColInfo>();
groupByInfoList.add(groupByInfo);
UserDimensionInfoToBuildQuery dim = new UserDimensionInfoToBuildQuery(dimensionName, columnList);
dimList.add(dim);
UserInfoToBuildQuery.Builder queryBuilder = new UserInfoToBuildQuery.Builder(dimList).
groupBy(groupByInfoList);
if (constraintList != null && !(constraintList.isEmpty())) {
if (constraintList.size() > 1) {
queryBuilder = queryBuilder.constraints(new UserComplexConstraint(constraintList,
ComplexConstraint.Joiner.AND));
} else {
queryBuilder = queryBuilder.constraints(constraintList.get(0));
}
}
List<Object> result = (List<Object>) DataAccessor.getData(queryBuilder.build());
if (result == null) {
continue;
}
JSONArray valueArray = new JSONArray();
for (Object row : result) {
List<Object> splitRow = (List<Object>) row;
valueArray.add(splitRow.get(0).toString());
}
dimObject.put(colName, valueArray);
}
finalObject.put(dimName, dimObject);
}
return finalObject;
}
}
Run Code Online (Sandbox Code Playgroud)
根据您的观察,JSON 遵循 JavaScript(或 ECMAScript,因为它应该被称为)保留字,而class就是其中之一。
在接触javascript时,似乎你可以在引号内使用保留字,我已经测试如下:
myObj = {"class" : "Analysis of Algorithms"}; // <--works
myObj = { class : "Analysis of Algorithms"}; // <--works
myObj = { class : "class"}; // <--works
myObj = {"class" : class }; // <--does not work.
Run Code Online (Sandbox Code Playgroud)
这很有趣,但我仍然对这个问题感到困惑。对于 JSON,我将所有技术与 javascript 结合使用,因此我无法产生与您相同的结果。
我在这个网站上找到了JSONObject.java的源文件