Why is json behaving in this weird manner?
java, json
Solution
As to follow up with your observation, JSON follows JavaScript (or ECMAScript, as it should be called) reserved words and class is one of them.
EDIT:
While dabbling with javascript, it seems you can use reserve words within quotation marks, I have tested it as follows:
myObj = {"class" : "Analysis of Algorithms"}; // <--works
myObj = { class : "Analysis of Algorithms"}; // <--works
myObj = { class : "class"}; // <--works
myObj = {"class" : class }; // <--does not work.
As well it is interesting, I am still puzzled by the problem. With JSON I am using all my techniques with javascript, so I cannot produce the same results as you can.
I found the source file for JSONObject.java at this website
Problem
Is there any reserve words for JSON as a KEY? my Json structure is ``` 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()); ``` OUTPUT: ``` dimObject#{"class":["A","B","c"],"name":["sam"]} finalObject#{"supplier":{"name":["sam"]}} ``` So the problem is why my json is behaving in a weird manner. I have not defined "class" as variable name, it's just a Key. Problem is ,if an argument is given like,"class" is a key or reserve word then how i successfully inserted in `Line#2`,and if its insert able in dimObject then why its not insert able in finalObject. Please help me solving this mystery EXACT CODE:: ``` 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; } } ```