Java: Casting Object to Array type

后端 未结 2 1481
栀梦
栀梦 2021-02-01 03:14

I am using a web service that returns a plain object of the type \"Object\". Debug shows clearly that there is some sort of Array in this object so I was wondering how I can cas

相关标签:
2条回答
  • 2021-02-01 03:20

    What you've got (according to the debug image) is an object array containing a string array. So you need something like:

    Object[] objects = (Object[]) values;
    String[] strings = (String[]) objects[0];
    

    You haven't shown the type of values - if this is already Object[] then you could just use (String[])values[0].

    Of course even with the cast to Object[] you could still do it in one statement, but it's ugly:

    String[] strings = (String[]) ((Object[])values)[0];
    
    0 讨论(0)
  • 2021-02-01 03:40

    Your values object is obviously an Object[] containing a String[] containing the values.

    String[] stringValues = (String[])values[0];
    
    0 讨论(0)
提交回复
热议问题