How to check whether the given object is object or Array in JSON string

后端 未结 4 1770
不思量自难忘°
不思量自难忘° 2020-12-05 08:04

I am getting JSON string from website. I have data which looks like this (JSON Array)

 myconf= {URL:[blah,blah]}

but some times this data c

4条回答
  •  -上瘾入骨i
    2020-12-05 08:59

    Quite a few ways.

    This one is less recommended if you are concerned with system resource issues / misuse of using Java exceptions to determine an array or object.

    try{
     // codes to get JSON object
    } catch (JSONException e){
     // codes to get JSON array
    }
    

    Or

    This is recommended.

    if (json instanceof Array) {
        // get JSON array
    } else {
        // get JSON object
    }
    

提交回复
热议问题