1

I need to convert a string to JSON and return the converted JSON. The thing is that the string might contain any json (object or array) and I do not know how to return a valid json in my listener. How can I call the listener when Java has two different classes (JSONArray and JSONObject)?

JSONArray obj = null;
String str;
try {
   str = new String(response.data); 

   obj = new JSONArray(str); // But this might be JSONObject as well!!! :-/

   if (listener != null) listener.OnDataLoaded(obj); // what Type should obj be?!?

} catch (JSONException e) {
    e.printStackTrace();
}
HoRn
  • 1,458
  • 5
  • 20
  • 25
ro.me
  • 11
  • 3
  • Would this help? https://stackoverflow.com/questions/6118708/determine-whether-json-is-a-jsonobject-or-jsonarray – HoRn Oct 21 '22 at 11:13
  • not really. The thing is I want to return a Json regardless of its content. I want to pass an argument to OnDataLoaded but I don't know how to write it. – ro.me Oct 21 '22 at 14:10
  • Can you paste your possible response.data structure or sample? I believe there would be an easy way out of it. – Jishnu Prathap Oct 30 '22 at 05:48
  • The reply might be: { "id": 14, "name": "Italy2 (imported)", "wheelbase": 0, "k_max": 0, "seed_point_angle": null, "first_middle_last": null } but it also might be: [ { "id": 14, "name": "Italy2 (imported)" }, { "id": 12, "name": "USA (imported)" } ] it depends on what request I send – ro.me Nov 04 '22 at 11:06

1 Answers1

0

First off, you should mention which JSON library you are using. It makes life easier for potential answerers. You seem to be using JSON-java (org.json). Is that correct?

Also, it is rather unusual for a program not to know what the structure of the received JSON is. That is one of the reasons JSON library often don't direct support what you want to do. You may want to look into making sure that there is no way to know more clearly what you are getting. Consider this: Once you know if you have an array or object, what is the next step? Do you know the further structure?

As mentioned, JSON-java is a library that doesn't provide a way to do this directly. Basically you can either just try to use JSONObject and catch the exception and then try JSONArray (or viceversa). Or you could look at the first (non-whitespace) character of the string to see if it's a { or a [.

Alternatively consider using a different JSON library. Jackson, for example, has a different data model structure where JsonObject and JsonObject inherit from a common JsonNode class which you parse into instead and which provides methods to check and access its properties independent from the actual implementation.

RoToRa
  • 37,635
  • 12
  • 69
  • 105
  • yes, correct. It's JSON-java (org.json) your solution (try JSONObject and then try JSON Array on catch) is exactly what I have implemented. The downside of this solution is I must create two different listeners for the derived class, which will take care of converting JSON into specific structures that depend on which is the derived class. Basically, I have this base class who implements the volley GET request and receives a JSON. I have different derived classes, one for each different GET request. – ro.me Nov 04 '22 at 11:01