Unity JNI,从 Android Native 获取 ArrayList<Hashtable<String,String>>



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

我可以从android原生库中获得bool和字符串,没有问题,但这个让我很困惑,

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字符串,从其中反序列化产品列表要方便得多。

相关内容

最新更新