Unity JNI,在C#中从Android Native获取ArrayList<Hashtable<String, String>>

Unity JNI, get ArrayList<Hashtable<String, String>> from Android Native in C#

谁能帮我在 Unity C# 中使用 ArrayList<Hashtable<String, String>>

我可以毫无问题地从 android 本机库中获取布尔值和字符串,但是这个让我很困惑,

public ArrayList<Hashtable<String, String>> getProducts()
{
  return pluginClass.CallStatic<ArrayList<Hashtable<String, String>>>("getProductList");
}

非常感谢。

using System.Collections.Generic;

public AndroidJavaOject getProducts()
{
   return pluginClass.CallStatic<AndroidJavaOject>("getProductList");
}

现在,当您在 C# 中调用 getProducts() 时,您会获得产品列表,它是产品的 java 表示。而下一步是如何将其解析成POCO,这显然是另一个问题。

通常不建议将返回的对象解析为 POCO,因为很难将来自 Java 的所有数据编组到 C# 而没有错误或异常。一种简单的方法是使用 Json:

这样的媒体

Java 边:

public String getProducts(){
    ObjectWriter ow = new  ObjectMapper().writer().withDefaultPrettyPrinter();
    String json = ow.writeValueAsString(productListObject); 
    return json;
}

在 C# 端

public void getProducts()
{
   string json = (string) pluginClass.CallStatic<AndroidJavaOject>("getProductList"); //or
   //string json = pluginClass.CallStatic<AndroidJavaOject>("getProductList").ToString(); 
   //You can use any json library to deserialize the json then
   Debug.Log(json); // Make sure this is correct string you wish to get
}

给定 json 字符串,从中反序列化产品列表要方便得多。