简体   繁体   中英

Java JSON converter “undefined for type” error

Using Eclipse, I'm converting data to JSON. I've downloaded the json.jar file and added it to my class path. I've imported these,

import org.json.JSONArray;
import org.json.JSONObject;

But when I try to use .add , .toJSONString and .contains I get errors like these,

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

Why is this happening? Here is the full code below.

   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

Yes, org.json.JSONArray does not have a method put(..) . See the JavaDoc.

You could use public JSONArray put(java.lang.Object value) .

Was using the wrong library, code works fine with JSONsimple ! :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM