You need to sign in to do that
Don't have an account?
Filip
Wrong method call?
Hello guys,
I am trying to call this method
public JSONObject getJSONObject(String key) { Object o = get(key); if (o instanceof JSONObject) { return (JSONObject)o; } throw new JSONException('JSONObject[' + quote(key) + '] is not a JSONObject.');
}
..declared under public class JSONObject. I am calling it with this code:
protected virtual List<SearchResult> parseSearchResults(String json) {
JSONObject j = new JSONObject(json);
JSONObject a = new j.getJSONObject('results');
...but keep getting compile error
Error: Compile Error: Invalid type: j.getJSONObject at line 221 column 28
Can you please help? I have tried all permutations I could imagine.
Many thanks F.
|
What you've written there doesn't make sense. First of all, you can't call new on a method. You can make a method in a class return itself if you want, although the actual contents of your method there don't make a lot of sense either.
If what you're trying to implement is a singleton, then it would probably be something like this:
public class JSONObject { static JSONObject theObject; public static JSONObject getJSONObject() { if (theObject==null) { theObject = new JSONObject(); } return theObject; } }
And then you'd get your singleton JSONObject by saying something like
JSONObject j = JSONObject.getJSONObject();
Ah -- now I see that you're using the JSONObject from the open source library.
So just make a new JSONObject, passing in a string containing the JSON you want to parse:
JSONObject j = new JSONObject(myJSONString);
and then call your methods on j, like
j.getString('results');
Hi Werewolf, thanks for picking up. I don't want to change the method definition, it's opensource that should work (http://developer.force.com/codeshare/apex/ProjectPage?id=a0630000002ahp4AAA).
I am trying to declare a JSONObject and assign it the (JSONObject) 'value' returned from method getJSONObject of (another JSONObject) j, called with parameter 'results'.
This is my crude version of it,
JSONObject a = new JSONObject('');a = j.getJSONObject('results');
but here compiler says 'Method does not exist or incorrect signature'.
Thanks for help.
F.
Well in your original post it said
a = new j.GetJSONObject('results');
The "new" was wrong.
In this case you can simply put
JSONObject a = j.getJSONObject('results');
Ok, that's what I had before one hour of tweaking. :) But both what I wrote and what you wrote give the same error -
JSONObject a = j.getJSONObject('results');
Compile Error: Method does not exist or incorrect signature: [JSONObject].getJSONObject(String) at line 221 column 24
I'm having the same problem. did u solve the problem? if so, pls let me know what have u done? thanks