/ /例外を与えるJson配列-android、arrays、json、soap

例外を与えるjson配列-android、arrays、json、soap

以下は私のJsonの応答です-

CallWMDefaultWithRef1Response{CallWMDefaultWithRef1Result=[{"ROLE_NAME":"","USER_MOBILE":"","IMEI_NUMBER1":,"USER_ID":30,"ROLE_ID":4,"USER_NAME":""}]; }

以下は私のコードです -

if (resultlog!= null) {

try {
JSONArray resultarray = new JSONArray(resultlog);
for (int i = 0; i < resultarray.length(); i++) {
JSONObject c = resultarray.getJSONObject(i);
roleName = c.getString("ROLE_NAME");
userMobile = c.getString("USER_MOBILE");
imeiNumber = c.getString("IMEI_NUMBER1");
userId = c.getString("USER_ID");
roleid = c.getString("ROLE_ID");
userNmae = c.getString("USER_NAME");
}
} catch (JSONException e) {
e.printStackTrace();
}
}

以下は私のエラーです

org.json.JSONException:タイプの値CallWMDefaultWithRef1Response java.lang.StringをJSONArrayに変換することはできません

回答:

回答№1は0

私のコードを試してください

try{
JSONObject result=resultlog.getJSONObject("CallWMDefaultWithRef1Response");
JSONArray resultarray=result.getJSONArray("CallWMDefaultWithRef1Result");
for(int i=0;i<resultarray.length();i++){
JSONObject c=resultarray.getJSONObject(i);
roleName=c.getString("ROLE_NAME");
userMobile=c.getString("USER_MOBILE");
imeiNumber=c.getString("IMEI_NUMBER1");
userId=c.getString("USER_ID");
roleid=c.getString("ROLE_ID");
userNmae=c.getString("USER_NAME");
}
} catch(JSONException e) {
e.printStackTrace();
}

回答№2の場合は0

結果ログの文字列は

CallWMDefaultWithRef1Response{CallWMDefaultWithRef1Result = [{"ROLE_NAME": ""、 "USER_MOBILE": ""、 "IMEI_NUMBER1" :、 "USER_ID":30、 "ROLE_ID":4、 "USER_NAME": ""}]; }

文字列が含まれています(CallWMDefaultWithRef1Response)JSONコンテンツと一緒に。 JSONコンテンツのみを新しいJSONArray()コンストラクターに渡す必要があります。

結果ログからJSON配列を抽出するには、

resultlog = resultlog.substring(jsonStr.indexOf("["), jsonStr.lastIndexOf("]")+1);

したがって、結果ログは次のようになります

[{"ROLE_NAME": ""、 "USER_MOBILE": ""、 "IMEI_NUMBER1" :、 "USER_ID":30、 "ROLE_ID":4、 "USER_NAME": ""}]

注:ここでは、「IMEI_NUMBER1」の後に文字値がありません。 これはJSONExceptionをスローします。サーバーでこれを修正する必要があります IMEI_NUMBER1の後に空の文字列を含めます。


回答№3の場合は0

あなたのエラーは文字列全体をとして使用しています JSONArray.

削除する CallWMDefaultWithRef1Response ログから。次に、新しいを作成します JSONObject 残っているものから、そして取得します JSONArray CallWMDefaultWithRef1Result オブジェクトからオブジェクトを取得するためのループとループ array.


回答№4の場合は0

resultLogの下は、実際のjsonの結果です。

    String roleName = "", userMobile = "", imeiNumber = "", userNmae = "";
int userId = 0, roleid = 0;

(または)

フローがjsonObjectとしてCallWMDefaultWithRef1Responseで始まる場合は、tryブロック内に以下のコード行を追加する必要があります。

    String resultlog= "{CallWMDefaultWithRef1Response:{CallWMDefaultWithRef1Result:{"ROLE_NAME":"","USER_MOBILE":"","IMEI_NUMBER1":"","USER_ID":30,"ROLE_ID":4,"USER_NAME":""}}}
JSONObject resultJsonObject = null;
try {
JSONObject obj1 = new JSONObject(resultlog);
JSONObject obj2 = obj1.has("CallWMDefaultWithRef1Response") ? obj1.getJSONObject("CallWMDefaultWithRef1Response") : null;
resultJsonObject = obj2.has("CallWMDefaultWithRef1Result") ? obj2.getJSONObject("CallWMDefaultWithRef1Result") : null;
roleName = resultJsonObject.has("ROLE_NAME") ? resultJsonObject.getString("ROLE_NAME") : "";
userMobile = resultJsonObject.has("USER_MOBILE") ? resultJsonObject.getString("USER_MOBILE") : "";
imeiNumber = resultJsonObject.has("IMEI_NUMBER1") ? resultJsonObject.getString("IMEI_NUMBER1") : "";
userId = resultJsonObject.has("USER_ID") ? resultJsonObject.getInt("USER_ID") : "";
roleid = resultJsonObject.has("ROLE_ID") ? resultJsonObject.getInt("ROLE_ID") : "";
userNmae = resultJsonObject.has("USER_NAME") ? resultJsonObject.getString("USER_NAME") : "";

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

それ以外の場合は、以下を使用してください。

     String resultlog = "{"ROLE_NAME":"","USER_MOBILE":"","IMEI_NUMBER1":"","USER_ID":30,"ROLE_ID":4,"USER_NAME":""}";
try {
JSONObject resultJsonObject = new JSONObject(resultlog);
roleName = resultJsonObject.has("ROLE_NAME") ? resultJsonObject.getString("ROLE_NAME") : "";
userMobile = resultJsonObject.has("USER_MOBILE") ? resultJsonObject.getString("USER_MOBILE") : "";
imeiNumber = resultJsonObject.has("IMEI_NUMBER1") ? resultJsonObject.getString("IMEI_NUMBER1") : "";
userId = resultJsonObject.has("USER_ID") ? resultJsonObject.getInt("USER_ID") : "";
roleid = resultJsonObject.has("ROLE_ID") ? resultJsonObject.getInt("ROLE_ID") : "";
userNmae = resultJsonObject.has("USER_NAME") ? resultJsonObject.getString("USER_NAME") : "";

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

回答№5の場合は0

私はこれを非常に簡単なステップで解決しました-

try {
JSONArray resultarray = new JSONArray(resultlog.getProperty(0));
for (int i = 0; i < resultarray.length(); i++) {
JSONObject c = resultarray.getJSONObject(i);
roleName = c.getString("ROLE_NAME");
userMobile = c.getString("USER_MOBILE");
imeiNumber = c.getString("IMEI_NUMBER1");
userId = c.getString("USER_ID");
roleid = c.getString("ROLE_ID");
userNmae = c.getString("USER_NAME");
}
} catch (JSONException e) {
e.printStackTrace();
}

他の人を助けるために私の答えを好きにしてください。

ハッピーコーディング..