1

Can anyone help me with using an ArrayList<Hashtable<String, String>> in Unity C#?

I can get bools and strings from an android native lib without issue but this one is throwing me,

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

Many thanks.

1 Answer 1

1
using System.Collections.Generic;

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

Now when you getProducts() is called in C#, you get the product list, which is java representation of the products. And the next step is how to parse it into POCO, which apparently is another question.

The parsing of the returned object into POCO is generally not recommended as it is hard to marshal all the data from Java to C# with no error or exceptions. An easy way is to use such medium as Json:

Java Side:

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

in C# side

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
}

Given the json string, it is much more convenient to desieralize the list of product out of it.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi mate, thanks for the help, this throws the following error, Cannot implicitly convert type UnityEngine.AndroidJavaObject' to System.Collections.Generic.List<System.Collections.Generic.Dictionary<string,string>>'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.