1

I create a java URL class which contain my Json data and have some function to obtain back my json data for doing some data comparison, I found out it's might not support by JSONObject for passing the data into the JSONObject. Do I need to use JSONArray in my case because my JSON data have array structure as well?

     try
           {
            JSONObject obj = new JSONObject (); 
            obj.readJsonFromUrl(theUrl);
            System.out.println(obj.toString());
           }

       catch(MalformedURLException e)
       {
           System.out.print("your problem here ...1");
       }
   }
   else
   {
       System.out.print("Can't Connect");
   }



I am sure that this is the place give me the error message because it return me this error in my compiler

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    The method readJsonFromUrl(URL) is undefined for the type JSONObject



there are also some warning message for that the JSONObject readJsonFromUrl method

private static JSONObject readJsonFromUrl(URL theUrl) throws IOException, JSONException {

Anyone can provide me the explaination of how the JSON data work in java? I saw quite number of Java class for JSON which make me confuse for it such as JSONObject, JSONArray , JSONValue. I search some information online but I also not very clear about it since I am very new to JSON data processing

This is my sample json data and the data I need is scan_result only

{  
   "data_id":"a71a3c2588c6472bb4daea41a0b58835",
   "file_info":{  
      "display_name":"",
      "file_size":242,
      "file_type":"Not available",
      "file_type_description":"Not available",
      "md5":"aa69ba384f22d0dc0551ace2fbb9ad55",
      "sha1":"09ceb54e65df3d3086b222e8643acffe451a6e8a",
      "sha256":"dcb46d6ae2a187f789c12f19c44bbe4b9a43bd200a3b306d5e9c1fcf811dc430",
      "upload_timestamp":"2016-11-18T09:09:08.390Z"
   },
   "process_info":{  
      "blocked_reason":"",
      "file_type_skipped_scan":false,
      "post_processing":{  
         "actions_failed":"",
         "actions_ran":"",
         "converted_destination":"",
         "converted_to":"",
         "copy_move_destination":""
      },
      "profile":"File scan",
      "progress_percentage":100,
      "result":"Allowed",
      "user_agent":""
   },
   "scan_results":{  
      "data_id":"a71a3c2588c6472bb4daea41a0b58835",
      "progress_percentage":100,
      "scan_all_result_a":"No Threat Detected",
      "scan_all_result_i":0,
      "scan_details":{  
         "Ahnlab":{  
            "def_time":"2016-11-08T15:00:00.000Z",
            "location":"local",
            "scan_result_i":0,
            "scan_time":1,
            "threat_found":""
         },
         "Avira":{  
            "def_time":"2016-11-08T00:00:00.000Z",
            "location":"local",
            "scan_result_i":0,
            "scan_time":133,
            "threat_found":""
         },
         "ClamAV":{  
            "def_time":"2016-11-08T10:28:00.000Z",
            "location":"local",
            "scan_result_i":0,
            "scan_time":94,
            "threat_found":""
         },
         "ESET":{  
            "def_time":"2016-11-08T00:00:00.000Z",
            "location":"local",
            "scan_result_i":0,
            "scan_time":38,
            "threat_found":""
         }
      },
      "start_time":"2016-11-18T09:09:08.405Z",
      "total_avs":4,
      "total_time":250
   },
   "vulnerability_info":{  

   }
} 
Heart Break KID
  • 119
  • 1
  • 2
  • 11

2 Answers2

0

You cannot pass json in url, you can pass it in body. Writing Json to stream body and post it using regular java method. Here is oracle community url of explanation of your problem.

Required Jar can be downloaded from here.

Test Code Follows:

   URL url = new URL("https://graph.facebook.com/search?q=java&type=post");
         try (InputStream is = url.openStream();
              JsonReader rdr = Json.createReader(is)) {

             JsonObject obj = rdr.readObject();
             JsonArray results = obj.getJsonArray("data");
             for (JsonObject result : results.getValuesAs(JsonObject.class)){
                 System.out.print(result.getJsonObject("from").getString("name"));
                 System.out.print(": ");
                System.out.println(result.getString("message", ""));
               System.out.println("-----------");
            }
        }
karman
  • 346
  • 3
  • 20
0

As mentioned here, there are many ways to solve this. Either you have to implement the read, parse operations yourself (@Roland Illig 's answer)

//you have to implement the readJSON method 
InputStream is = new URL(url).openStream();
try {
  BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
  String jsonText = readAll(rd);
  JSONObject json = new JSONObject(jsonText);
  return json;
} finally {
  is.close();
}

Or you could use a library. The most well-known and widely used libraries are jackson and gson. The big picture is that you try to "map" your json Object to a class.

You have your json file:

{ "id":1, "name":"eirini", "hobbies":["music","philosophy","football"] }

and a class that represents this file and will store the values (depending on the library that you use there might be different requirements, for example getters, setters etc..)

public class Person {
   public int id;
   public String name;
   public List<String> hobbies = new ArrayList<String>();

   public String toString() {
       return name +" has the id: " + id + " the following hobbies" + hobbies.get(0) + " " + hobbies.get(2); 
   }
}

Finally in your main method:

  public static void main(String[] args) throws IOException, ParseException {
    ObjectMapper mapper = new ObjectMapper();
    InputStream input = this.getClass().getResourceAsStream(FILE); //read your file. There are many ways to achieve this.
    ObjectMapper mapper = new ObjectMapper(); // just need one
    Person eirini = mapper.readValue(input, Person.class);
    System.out.println(eirini.toString());
Community
  • 1
  • 1
Eirini Graonidou
  • 1,506
  • 16
  • 24