gpt4 book ai didi

java - 为什么 json 以这种奇怪的方式表现?

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:21:41 24 4
gpt4 key购买 nike

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());

输出:

dimObject#{"class":["A","B","c"],"name":["sam"]}
finalObject#{"supplier":{"name":["sam"]}}

所以问题是为什么我的 json 行为异常。
我没有将“类”定义为变量名,它只是一个键。

问题是,如果给出一个参数,例如,"class"is a key or reserve word then how i successfully inserted in 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;
}
}

最佳答案

根据您的观察,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.

虽然很有趣,但我仍然对这个问题感到困惑。对于 JSON,我将我的所有技术都与 javascript 结合使用,因此我无法产生与您相同的结果。

我找到了 JSONObject.java 的源文件在 this website

关于java - 为什么 json 以这种奇怪的方式表现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2111635/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com