- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我的 java 程序像这样将 JSONObject 传递给 native 程序:
import org.json.simple.JSONObject;
class json{
static{
System.loadLibrary("json");
}
private native void json(JSONObject j);
public static void main(String args[]){
json js = new json();
JSONObject j = new JSONObject();
j.put("name","someone");
j.put("age", 15);
js.json(j);
}
}
如何在c中访问这些数据?
#include<stdio.h>
#include<jni.h>
#include "json.h"
#include "cJSON.h"
JNIEXPORT void JNICALL Java_json_json(JNIEnv *env, jobject obj, jobject j){
return;
}
我找不到任何关于如何在 c 中从 jobject 访问 json 数据的引用资料。我是 jni 的新手。还有其他方法吗?
最佳答案
通过 JNI 从 C 代码访问 JSON 数据与通过 Java 访问它没有太大区别。在 Java 中你会这样写:
System.out.println("name = " + j.get("name"));
System.out.println("age = " + j.get("age"));
C 代码基本相同,只是更加冗长,因为
get
的键构造 jstring
实例j.get("name")
获取的jstring
转换为C 字符串java.lang.Integer
下面的代码就是这样做的:
#include<stdio.h>
#include<jni.h>
#include "json.h"
// Notice that "cJSON.h" is not needed
JNIEXPORT void JNICALL Java_json_json(JNIEnv *env, jobject obj, jobject json) {
// Get the Class object for JSONObject.
jclass JSONObjectClass = (*env)->GetObjectClass(env, json);
// Get the ID of the "get" method that we must call.
// Notice that due to type erasure, both the parameter and the return value
// are just plain Objects in the type signature.
jmethodID getID = (*env)->GetMethodID(env, JSONObjectClass, "get", "(Ljava/lang/Object;)Ljava/lang/Object;");
if (getID == NULL) return;
// Create java.lang.String instances for the keys we pass to j.get.
jstring nameKey = (*env)->NewStringUTF(env, "name");
jstring ageKey = (*env)->NewStringUTF(env, "age");
if (nameKey == NULL || ageKey == NULL) return;
// Actually get the name object. Since we know that the value added
// in the Java code is a string, we just cast the jobject to jstring.
jstring name = (jstring) (*env)->CallObjectMethod(env, json, getID, nameKey);
if (name == NULL) return;
// Get a C string that can be used with the usual C functions.
const char *name_cstr = (*env)->GetStringUTFChars(env, name, NULL);
if (name_cstr == NULL) return;
printf("name = %s\n", name_cstr);
// Once done, the string must be released.
(*env)->ReleaseStringUTFChars(env, name, name_cstr);
name_cstr = NULL;
// Get the java.lang.Integer object representing the age.
jobject ageObj = (*env)->CallObjectMethod(env, json, getID, ageKey);
if (ageObj == NULL) return;
// Unbox the java.lang.Integer manually.
jclass IntegerClass = (*env)->GetObjectClass(env, ageObj);
jmethodID intValueID = (*env)->GetMethodID(env, IntegerClass, "intValue", "()I");
if (intValueID == NULL) return;
jint age = (*env)->CallIntMethod(env, ageObj, intValueID);
printf("age = %d\n", (int) age);
}
更复杂的使用以相同的方式完成:检查您将用 Java 编写的内容,并找到一种方法来使用 JNI functions 在 C 中调用具有相同值的相同方法。 .
关于c - 如何在 jni 中访问传递给 c 的 JSON 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56748833/
我刚开始使用 JNI,但遇到以下问题。 我有一个包含简单类的 C++ 库。我从 Java Android 项目中调用了三个 JNI 方法,分别实例化所述类、调用实例化类的方法并销毁它。我保留了对该对象
背景 我有一个 android 项目,它使用 JNI(使用 NDK)以 Java 和 C/C++ 进行编码。 我在java端创建了一个Jni java包装器,它将自己完成所有Jni操作,而除了这个包装
我想传递一个java对象的地址,JNI方法应该填充所传递对象的字段。1. java代码传递给定的对象引用。 JNI 方法应该能够缓存这个对象。这是一次性通话。2. 然后,java 对象使用不同的参数重
我本质上是在尝试遵循描述的 SO 解决方案 here , 但我遇到了问题。 这基本上就是我在 JNI 库中所做的事情: handle = dlopen("/data/data/lib/my.packa
我试图实现某种异常处理 一开始我打电话 jni::ExceptionDescribe() 之后我使用了的实现 How to obtain a description of a Java excepti
是否可以从不同 jni 库的另一个方法调用 1 个库的 jni 方法?例如:我有 2 个库 lib_1.so 和 lib_2.so。 我想从 lib_2.so 调用 lib_1.so 的方法 get_
我想在native方法中使用动态注册,所以我需要设置JNI_onLoad功能。我只是写了一个函数来获取两个数字的总和。但是,它无法正确构建。我该如何更正错误? 这是我的 *.cpp 文件,我将此文件命
我已经为 C 头文件制作了一个 make 文件,它工作正常,但是说 JNICALL 和 JNIEnv 存在语法错误,但我已经弄明白了这是因为头文件中的类型。 Image of the failure
我需要实现一个本地方法,比方说“public native void someFunc();”。我有两个库,libabc.so 和 libdef.so。 Java 使用 System.loadLibr
背景 我正在 eclipse 中为 android 开发一个应用程序,现在我遇到了一个问题,我需要你的帮助。所以我必须从 JAVA 应用程序调用用 C 编写的函数。但是在我编写代码的过程中,我有一些问
我正在使用 Android 上的 Java native 接口(interface)将当前 Activity 传递给 native 方法。但我没有使用类似 JNI 的函数名称来执行此操作。我正在手动注
我正在使用 JNI,我想知道是否可以通过 delegate 进行通信。 例如: Kotlin typealias MessageReceived = (msg: String) -> Unit ext
看来我对 JNI 的运气并不好。我正在等待我买的书到货,但现在是试错法。 我正在使用 JNI 来实现 Lua 求值器。 evaluatorNew() 只是创建一个新的 Evaluator() 对象,创
我有下面的代码,我想调用在同一个源文件中实现的函数,在本例中使用 C 语言: JNIEXPORT jstring JNICALL MyClass_get_1Uname__C (JNIEnv *env,
硬件手机和平板电脑内存太少,但 HAXM 工作正常? 我的基本问题是我的应用程序(很可能是我的 Java 应用程序加载的 JNI 动态库)太大。如果未使用硬件电话和模板调用 JNI 指令,Java 应
我正面临崩溃 JNI WARNING : 0x44f81e80 is not a valid JNI reference, in Ldalvik/system/NativeStart;. run()v
我移植了很多数学知识。我正在使用 over to c++ from java 并看到这样做有很大的性能提升,但我无法弄清楚要使用什么 jni 函数来摆脱我不再需要的变量。例如,我知道当您的 jni 方
我正在使用 JNI 调用一个静态 java 方法,该方法又创建一个 Swing JFrame 并显示它。代码相当简单,Java 代码独立运行(即 java StartAWT 做它应该做的事),而当使用
我正在尝试创建一个新线程,因此我将 VM 从我的方法初始化(从 Java 调用)传递到我的新线程。在线程中,我调用 AttachCurrentThread 并获取 JNIEnv* env。 稍后,我尝
我想知道是否有可能从java调用C++方法。 我非常希望能够从 java 读取内存进程。 我懂c++,但我想使用像java这样的更高级别,但仍然能够侵入进程内存。 有什么线索吗? []的 最佳答案 这
我是一名优秀的程序员,十分优秀!