jsonobject无法使用多种输入方式进行转换

时间:2014-02-03 19:38:26

标签: android json

我做了一个asynctask例程来通信我所有的web服务,如下所示:

  protected String[] doInBackground(String... params) {

String parameterString = params[0];

        // effectieve http request met de parameters toegevoegd
HttpClient client = new DefaultHttpClient();
HttpGet aanvraag = new HttpGet(server + parameterString);

// foutanalyse van de http request
try
{
    HttpResponse antwoord = client.execute(aanvraag);
    StatusLine statuslijn = antwoord.getStatusLine();
    int statuscode = statuslijn.getStatusCode();
    if(statuscode != 200){
        Log.i("statuscode verzending", "statuscode= "+ statuscode);
        return null;
    }

    InputStream jsonStream = antwoord.getEntity().getContent();
    BufferedReader reader= new BufferedReader(new InputStreamReader(jsonStream));
    StringBuilder builder = new StringBuilder();
    String lijn;

    while((lijn = reader.readLine())!= null){
        builder.append(lijn);

    }

    String jsonData = builder.toString();

    // hier beginnen we met de json data te ontmantelen
    JSONArray json = new JSONArray(jsonData);



    String[] data = new String[35];

    Log.i("jsonparser", "lengte geretourde data " + json.length());
    for (int i = 0; i < json.length(); i++) 
    {
        data[i] = json.getString(i).toString();
    }


    return data;

}
catch (ClientProtocolException e){

    e.printStackTrace();
}
catch (IOException e){

    e.printStackTrace();
}
 catch (JSONException e) {

e.printStackTrace();
  }
        return null;
    }

我每次连接到我的应用程序的web服务时都会使用例程,(我有大约15个) 所有这些Web服务都由我的软件供应商制作的程序制作(和使用)

现在我想用我的Android应用程序连接到它们 其中一些正在发挥作用 别人不这样做 我在寻找这个失败时已经发现的东西:

某些jsondata以以下形式返回:

    [
  "42416",
  " ",
   " ",
  " "
    ]

其他web服务将数据返回为:

    true

但我最喜欢的那些看起来像:

    {
   "z00": "1 ",
   "z01": 10000,
   "z02": "18/06/2010",
   "z03": "A",
   "z04": "0000",
   "z05": 7735,
   "z06": "VANNUYSE BVBA",                                                                                           
   "z07": "DEEFA",
   "z08": 17170,
   "z09": "AFLEVEREN HELI  IN GEBRUIK",
   "z10": "0000",
   "z11": "8770    ",
   "z12": "INTER                  ",
   "z13": "HELI                ",
   "z14": "CPCD25 - C240       ",
   "z15": "48182               ",
   "z16": "",
   "z17": "N",
   "z18": "0030",
   "z19": 0,
   "z20": "X",
   "z21": "                              ",
   "z22": "J",
   "z23": "",
   "z24": 0,
   "z25": "22/06/2010",
   "z26": 16854,
   "z27": 0,
   "z28": "AFLEVEREN IN GEBRUIK",
   "z29": "               "
   }

我应该如何形成我的asynctask例程,以便能够为所有例程工作?

因为我的例程现在对第一种类型的数据工作正常但是在第3种类型我得到一个错误(第二种类型没有尝试过):

JSONObject cannot be converted to JSONArray

感谢你的帮助人员

1 个答案:

答案 0 :(得分:1)

如果您不知道(jsonObject或jsonArray)将要发生什么,您可以尝试这样的事情:

try {
    JSONObject jsonObject = new JSONObject(jsonData);
    // handle jsonObject 
} catch (JSONException e) {
    // Try to parse it to a JSONAray. 
    // Also you can check the exception message here to determine if jsonArray conversion is wanted
    try {
        JSONArray jsonArray = new JSONArray(jsonData);
        // handle jsonArray             
    } catch (JSONException e) {
        // something which is not JsonObject or JsonArray! 
    }
}

或试试这个:

Object jsonAsObject = new JSONTokener(jsonData).nextValue();

if (jsonAsObject instanceof JSONObject) {
    // it is JSONObject
    JSONObject jsonObject = (JSONObject) jsonAsObject;  
} else if (jsonAsObject instanceof JSONArray) {
    // it is JSONArray
    JSONArray jsonArray = (JSONArray) jsonAsObject;
}