【问题标题】:Unity JNI, get ArrayList<Hashtable<String, String>> from Android Native in C#Unity JNI,在 C# 中从 Android Native 获取 ArrayList<Hashtable<String, String>>
【发布时间】:2015-10-07 22:21:48
【问题描述】:

谁能帮我在 Unity C# 中使用 ArrayList&lt;Hashtable&lt;String, String&gt;&gt;

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

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

非常感谢。

【问题讨论】:

    标签: java c# unity3d java-native-interface hashtable


    【解决方案1】:
    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 字符串,从中反序列化产品列表会更方便。

    【讨论】:

    • 嗨伙计,感谢您的帮助,这会引发以下错误,无法隐式转换类型 UnityEngine.AndroidJavaObject' to System.Collections.Generic.List> '
    猜你喜欢
    • 2016-08-23
    • 2013-05-27
    • 1970-01-01
    • 1970-01-01
    • 2013-07-06
    • 2020-02-20
    • 2015-07-08
    • 2014-08-15
    • 2013-10-05
    相关资源
    最近更新 更多