Java JSON 转换器 "undefined for type" 错误
Java JSON converter "undefined for type" error
我正在使用 Eclipse 将数据转换为 JSON。我已经下载了 json.jar
文件并将其添加到我的 class 路径中。我已经导入了这些,
import org.json.JSONArray;
import org.json.JSONObject;
但是当我尝试使用 .add
、.toJSONString
和 .contains
时,我得到了这样的错误,
The method add(String) is undefined for the type JSONArray
The method add(JSONObject) is undefined for the type JSONArray
The method toJSONString() is undefined for the type JSONObject
为什么会这样?下面是完整的代码。
private JSONObject getJson(){
JSONObject obj = new JSONObject();
String forename = null;
String surname = null;
JSONArray nodes = new JSONArray();
JSONArray links = new JSONArray();
Map<Integer,Integer> sourceMap = new HashMap<Integer, Integer>();
Map<Integer, Integer> targetMap = new HashMap<Integer, Integer>();
int linkSourceActivity = -1;
int linkTargetActivity = -1;
String activity =null;
int sourceId = -1;
int targetId = -1;
int nodeIndex = 0;
int targetIndex = -1;
JSONObject jsonLine = null;
// Iterate over the Map of links
for(Integer index : this.links.keySet()){
// gather information needed for Json object
Link link = this.links.get(index);
// nodes need
sourceId = link.getSourceId();
targetId = link.getTargetId();
forename = people.getValue(link.getSourceId()).getForenames();
surname = people.getValue(link.getSourceId()).getSurname();
linkSourceActivity = link.getSourceActivityCode();
//If source node doesn't exist, create it
if(!sourceMap.containsKey(sourceId)){
// Create a node and add to array
jsonLine = new JSONObject();
jsonLine.put("name",forename+ " " +surname);
jsonLine.put("group", linkSourceActivity);
jsonLine.put("role", "Director");
nodes.add(jsonLine);
// add entry to map
sourceMap.put(sourceId, nodeIndex);
nodeIndex++;
}
forename = people.getValue(link.getTargetId()).getForenames();
surname = people.getValue(link.getTargetId()).getSurname();
linkTargetActivity = link.getTargetActivityCode();
activity = link.getTargetActivity();
targetIndex = (targetId * 0x1f1f1f1f) ^ linkTargetActivity;
// If the target node doesn't exist, create it
if(!targetMap.containsKey(targetIndex)){
jsonLine = new JSONObject();
jsonLine.put("name", forename+ " " +surname);
jsonLine.put("group", linkTargetActivity);
jsonLine.put("role", activity);
nodes.add(jsonLine);
// add entry to map
targetMap.put(targetIndex, nodeIndex);
nodeIndex++;
}
// links need
// source (position in node array)
// target (position in node array)
int sourcePos = sourceMap.get(sourceId);
int targetPos = targetMap.get(targetIndex);
int value = link.getMultiplicity();
// Build the array of play titles for this Link
List<Integer> productionIds = link.getProductions();
JSONArray playTitles = new JSONArray();
int playId = -1;
String playName = null;
for (Integer Id : productionIds){
playId = productions.getValue(Id).getPlayId();
playName = plays.getValue(playId).getMainTitle();
// don't include duplicate titles
if(!playTitles.contains(playName)){
playTitles.add(plays.getValue(playId).getMainTitle());
}
}
JSONObject linkLine = new JSONObject();
linkLine.put("source", sourcePos);
linkLine.put("target", targetPos);
linkLine.put("value", value);
linkLine.put("plays", playTitles);
links.add(linkLine);
} // end iterate over Links
obj.put("nodes", nodes);
obj.put("links", links);
return obj;
} // end getJson method
// Method to write Json to a file
public void writeJson(String path){
try {
FileWriter file = new FileWriter(path);
file.write(jsonLinks.toJSONString());
file.flush();
file.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} // end JsonOutput
是的,org.json.JSONArray
没有方法 put(..)
。请参阅 JavaDoc。
使用了错误的库,代码在 JSONsimple 中运行良好! :)
我正在使用 Eclipse 将数据转换为 JSON。我已经下载了 json.jar
文件并将其添加到我的 class 路径中。我已经导入了这些,
import org.json.JSONArray;
import org.json.JSONObject;
但是当我尝试使用 .add
、.toJSONString
和 .contains
时,我得到了这样的错误,
The method add(String) is undefined for the type JSONArray
The method add(JSONObject) is undefined for the type JSONArray
The method toJSONString() is undefined for the type JSONObject
为什么会这样?下面是完整的代码。
private JSONObject getJson(){
JSONObject obj = new JSONObject();
String forename = null;
String surname = null;
JSONArray nodes = new JSONArray();
JSONArray links = new JSONArray();
Map<Integer,Integer> sourceMap = new HashMap<Integer, Integer>();
Map<Integer, Integer> targetMap = new HashMap<Integer, Integer>();
int linkSourceActivity = -1;
int linkTargetActivity = -1;
String activity =null;
int sourceId = -1;
int targetId = -1;
int nodeIndex = 0;
int targetIndex = -1;
JSONObject jsonLine = null;
// Iterate over the Map of links
for(Integer index : this.links.keySet()){
// gather information needed for Json object
Link link = this.links.get(index);
// nodes need
sourceId = link.getSourceId();
targetId = link.getTargetId();
forename = people.getValue(link.getSourceId()).getForenames();
surname = people.getValue(link.getSourceId()).getSurname();
linkSourceActivity = link.getSourceActivityCode();
//If source node doesn't exist, create it
if(!sourceMap.containsKey(sourceId)){
// Create a node and add to array
jsonLine = new JSONObject();
jsonLine.put("name",forename+ " " +surname);
jsonLine.put("group", linkSourceActivity);
jsonLine.put("role", "Director");
nodes.add(jsonLine);
// add entry to map
sourceMap.put(sourceId, nodeIndex);
nodeIndex++;
}
forename = people.getValue(link.getTargetId()).getForenames();
surname = people.getValue(link.getTargetId()).getSurname();
linkTargetActivity = link.getTargetActivityCode();
activity = link.getTargetActivity();
targetIndex = (targetId * 0x1f1f1f1f) ^ linkTargetActivity;
// If the target node doesn't exist, create it
if(!targetMap.containsKey(targetIndex)){
jsonLine = new JSONObject();
jsonLine.put("name", forename+ " " +surname);
jsonLine.put("group", linkTargetActivity);
jsonLine.put("role", activity);
nodes.add(jsonLine);
// add entry to map
targetMap.put(targetIndex, nodeIndex);
nodeIndex++;
}
// links need
// source (position in node array)
// target (position in node array)
int sourcePos = sourceMap.get(sourceId);
int targetPos = targetMap.get(targetIndex);
int value = link.getMultiplicity();
// Build the array of play titles for this Link
List<Integer> productionIds = link.getProductions();
JSONArray playTitles = new JSONArray();
int playId = -1;
String playName = null;
for (Integer Id : productionIds){
playId = productions.getValue(Id).getPlayId();
playName = plays.getValue(playId).getMainTitle();
// don't include duplicate titles
if(!playTitles.contains(playName)){
playTitles.add(plays.getValue(playId).getMainTitle());
}
}
JSONObject linkLine = new JSONObject();
linkLine.put("source", sourcePos);
linkLine.put("target", targetPos);
linkLine.put("value", value);
linkLine.put("plays", playTitles);
links.add(linkLine);
} // end iterate over Links
obj.put("nodes", nodes);
obj.put("links", links);
return obj;
} // end getJson method
// Method to write Json to a file
public void writeJson(String path){
try {
FileWriter file = new FileWriter(path);
file.write(jsonLinks.toJSONString());
file.flush();
file.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} // end JsonOutput
是的,org.json.JSONArray
没有方法 put(..)
。请参阅 JavaDoc。
使用了错误的库,代码在 JSONsimple 中运行良好! :)