How to create this type of block of json utils

How to create this type of block of json utils

component_method

I want learn. :pray:

With simple json library it would look something like this:

@SimpleFunction
public Object GetValue(String object,String key){
    JSONObject jObject = (JSONObject) new JSONParser().parse(object);
    return jObject.get(key);
}

However, if you are just starting with Java then it is recommended to not mess up with json.

3 Likes

Library??
I don’t think it need any library

1 Like

Why do you think so?
I don’t trust the json lib included in sources.
json simple library is just 16 kb in size.

1 Like

@vknow360 I had used JSON in my first extension for parsing the data from URL and it didn’t used any library

Tell me about its library.
what we import.

Yes, org.json comes bundled with app inventor sources.
But that contains more than what is needed for using json.


https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple/1.1
Add this to /lib/deps if you are using extension template.

Here’s my code

 @SimpleFunction
public Object GetValue(String object,String key){
    JSONObject object=new JSONObject(object);
    return object.getString(key);
}  

But it’s better than using a library

You are still using a library.
And that is actually going to increase your extension’s size by 50+ kb.

1 Like

JSONObject is a class org.json package.
It does not seem to fall under Java packages.

1 Like

I got an error from using this code

Buildfile: /projects/goldv2/appinventor-sources/appinventor/build.xml

extensions:

clean:
[delete] Deleting directory /projects/goldv2/appinventor-sources/appinventor/components/build

BUILD FAILED
/projects/goldv2/appinventor-sources/appinventor/build.xml:34: The following error [delete] Deleting directory /projects/goldv2/appinventor-sources/appinventor/components/reports

init:

common_CommonUtils:

init:

CommonUtils:

common_CommonVersion:

init:

CommonVersion:
[exec] Result: 128
[exec] Result: 128

CopyToRunLibDir:

components_AndroidRuntime:

init:
[mkdir] Created dir: /projects/goldv2/appinventor-sources/appinventor/build/components
[mkdir] Created dir: /projects/goldv2/appinventor-sources/appinventor/components/build
[mkdir] Created dir: /projects/goldv2/appinventor-sources/appinventor/components/build/classes
[mkdir] Created dir: /projects/goldv2/appinventor-sources/appinventor/components/reports
[mkdir] Created dir: /projects/goldv2/appinventor-sources/appinventor/components/reports/raw
[mkdir] Created dir: /projects/goldv2/appinventor-sources/appinventor/components/reports/html

CommonConstants:
[mkdir] Created dir: /projects/goldv2/appinventor-sources/appinventor/components/build/classes/CommonConstants
[javac] Compiling 6 source files to /projects/goldv2/appinventor-sources/appinventor/components/build/classes/CommonConstants
[javac] warning: [options] bootstrap class path not set in conjunction with -source 1.7
[javac] 1 warning
[javac] Creating empty /projects/goldv2/appinventor-sources/appinventor/components/build/classes/CommonConstants/com/google/appinventor/components/common/package-info.class
[jar] Building jar: /projects/goldv2/appinventor-sources/appinventor/build/components/CommonConstants.jar
[jar] Building jar: /projects/goldv2/appinventor-sources/appinventor/build/components/CommonConstants-gwt.jar

HtmlEntities:
[mkdir] Created dir: /projects/goldv2/appinventor-sources/appinventor/components/build/classes/HtmlEntities
[javac] Compiling 1 source file to /projects/goldv2/appinventor-sources/appinventor/components/build/classes/HtmlEntities
[javac] warning: [options] bootstrap class path not set in conjunction with -source 1.7
[javac] 1 warning
[jar] Building jar: /projects/goldv2/appinventor-sources/appinventor/components/build/HtmlEntities.jar

common_CommonVersion:

init:

CommonVersion:
[exec] Result: 128
[exec] Result: 128

AndroidRuntime:
[mkdir] Created dir: /projects/goldv2/appinventor-sources/appinventor/components/build/classes/AndroidRuntime
[javac] Compiling 332 source files to /projects/goldv2/appinventor-sources/appinventor/components/build/classes/AndroidRuntime
[javac] warning: [options] bootstrap class path not set in conjunction with -source 1.7
[javac] /projects/goldv2/appinventor-sources/appinventor/components/src/com/appybuilder/chand_nami_2000/Request/Request.java:101: error: variable object is already defined in method GetValue(String,String)
[javac] JSONObject object = (JSONObject) new JSONParser().parse(object);
[javac] ^
[javac] /projects/goldv2/appinventor-sources/appinventor/components/src/com/appybuilder/chand_nami_2000/Request/Request.java:101: error: cannot find symbol
[javac] JSONObject object = (JSONObject) new JSONParser().parse(object);
[javac] ^
[javac] symbol: class JSONParser
[javac] location: class Request
[javac] Note: Some input files use or override a deprecated API.
[javac] Note: Recompile with -Xlint:deprecation for details.
[javac] Note: Some input files use unchecked or unsafe operations.
[javac] Note: Recompile with -Xlint:unchecked for details.
[javac] 2 errors
[javac] 1 warning

BUILD FAILED
/projects/goldv2/appinventor-sources/appinventor/build.xml:35: The following error occurred while executing this line:
/projects/goldv2/appinventor-sources/appinventor/build-common.xml:372: The following error occurred while executing this line:
/projects/goldv2/appinventor-sources/appinventor/components/build.xml:141: The following error occurred while executing this line:
/projects/goldv2/appinventor-sources/appinventor/build-common.xml:118: Compile failed; see the compiler error output for details.

Total time: 4 seconds

@Nami_Chand have you imported the library jar file in your extension as @vknow360 said

No, I am creating extension in AppyBuilder Code Editor.

Tell me how to import.

You can’t add library in appybuilder code editor then why you have used this code

1 Like

Tell me how to import lib in computer software.

Then you should use this:

1 Like

I got an error from using this code

Buildfile: /projects/goldv2/appinventor-sources/appinventor/build.xml

extensions:

clean:
[delete] Deleting directory /projects/goldv2/appinventor-sources/appinventor/components/build

BUILD FAILED
/projects/goldv2/appinventor-sources/appinventor/build.xml:34: The following error [delete] Deleting directory /projects/goldv2/appinventor-sources/appinventor/components/reports

init:

common_CommonUtils:

init:

CommonUtils:

common_CommonVersion:

init:

CommonVersion:
[exec] Result: 128
[exec] Result: 128

CopyToRunLibDir:

components_AndroidRuntime:

init:
[mkdir] Created dir: /projects/goldv2/appinventor-sources/appinventor/build/components
[mkdir] Created dir: /projects/goldv2/appinventor-sources/appinventor/components/build
[mkdir] Created dir: /projects/goldv2/appinventor-sources/appinventor/components/build/classes
[mkdir] Created dir: /projects/goldv2/appinventor-sources/appinventor/components/reports
[mkdir] Created dir: /projects/goldv2/appinventor-sources/appinventor/components/reports/raw
[mkdir] Created dir: /projects/goldv2/appinventor-sources/appinventor/components/reports/html

CommonConstants:
[mkdir] Created dir: /projects/goldv2/appinventor-sources/appinventor/components/build/classes/CommonConstants
[javac] Compiling 6 source files to /projects/goldv2/appinventor-sources/appinventor/components/build/classes/CommonConstants
[javac] warning: [options] bootstrap class path not set in conjunction with -source 1.7
[javac] 1 warning
[javac] Creating empty /projects/goldv2/appinventor-sources/appinventor/components/build/classes/CommonConstants/com/google/appinventor/components/common/package-info.class
[jar] Building jar: /projects/goldv2/appinventor-sources/appinventor/build/components/CommonConstants.jar
[jar] Building jar: /projects/goldv2/appinventor-sources/appinventor/build/components/CommonConstants-gwt.jar

HtmlEntities:
[mkdir] Created dir: /projects/goldv2/appinventor-sources/appinventor/components/build/classes/HtmlEntities
[javac] Compiling 1 source file to /projects/goldv2/appinventor-sources/appinventor/components/build/classes/HtmlEntities
[javac] warning: [options] bootstrap class path not set in conjunction with -source 1.7
[javac] 1 warning
[jar] Building jar: /projects/goldv2/appinventor-sources/appinventor/components/build/HtmlEntities.jar

common_CommonVersion:

init:

CommonVersion:
[exec] Result: 128
[exec] Result: 128

AndroidRuntime:
[mkdir] Created dir: /projects/goldv2/appinventor-sources/appinventor/components/build/classes/AndroidRuntime
[javac] Compiling 332 source files to /projects/goldv2/appinventor-sources/appinventor/components/build/classes/AndroidRuntime
[javac] warning: [options] bootstrap class path not set in conjunction with -source 1.7
[javac] /projects/goldv2/appinventor-sources/appinventor/components/src/com/appybuilder/chand_nami_2000/Request/Request.java:101: error: variable object is already defined in method GetValue(String,String)
[javac] JSONObject object=new JSONObject(object);
[javac] ^
[javac] /projects/goldv2/appinventor-sources/appinventor/components/src/com/appybuilder/chand_nami_2000/Request/Request.java:101: error: no suitable constructor found for JSONObject(JSONObject)
[javac] JSONObject object=new JSONObject(object);
[javac] ^
[javac] constructor JSONObject.JSONObject(Map) is not applicable
[javac] (argument mismatch; JSONObject cannot be converted to Map)
[javac] constructor JSONObject.JSONObject(JSONTokener) is not applicable
[javac] (argument mismatch; JSONObject cannot be converted to JSONTokener)
[javac] constructor JSONObject.JSONObject(String) is not applicable
[javac] (argument mismatch; JSONObject cannot be converted to String)
[javac] Note: Some input files use or override a deprecated API.
[javac] Note: Recompile with -Xlint:deprecation for details.
[javac] Note: Some input files use unchecked or unsafe operations.
[javac] Note: Recompile with -Xlint:unchecked for details.
[javac] Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
[javac] 2 errors
[javac] 1 warning

BUILD FAILED
/projects/goldv2/appinventor-sources/appinventor/build.xml:35: The following error occurred while executing this line:
/projects/goldv2/appinventor-sources/appinventor/build-common.xml:372: The following error occurred while executing this line:
/projects/goldv2/appinventor-sources/appinventor/components/build.xml:141: The following error occurred while executing this line:
/projects/goldv2/appinventor-sources/appinventor/build-common.xml:118: Compile failed; see the compiler error output for details.

Total time: 4 seconds

Try this:

@SimpleFunction()
    public Object GetValue(String object,String key){
        try {
            org.json.JSONObject jsonObject = new org.json.JSONObject(object);
            return jsonObject.get(key);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "";
    }

ok! I had imported this class.
now build is pending.