获取此错误org.json.JSONException:类型为java.lang.String的0处的值无法转换为JSONObjecton解析

问题描述 投票:1回答:2

我在解析json时收到此错误。任何人都可以帮我解决这个问题。

  {
"website": [
    "http://www.example.com",
    "https://buy.example.com"
],
"number": [
    "4546",
    "54256456"
],
"email": [
    "[email protected]",
    "[email protected]"
],
"address": [
    "any adddress here",
    "Address 2"
]
  }

任何人都可以帮我解析这个json ...

Json解析代码

             String JSON_STRING = response.body().string();
                    Log.e("json_string", JSON_STRING.toString());
                    //Toast.makeText( getApplicationContext(), "Response : "+JSON_STRING, Toast.LENGTH_LONG ).show();

                    JSONObject object = new JSONObject( JSON_STRING );
                    JSONArray jsonArray = object.getJSONArray( "website" );

                        for (int i=0; i<jsonArray.length(); i++)
                        {
                            JSONObject website = jsonArray.getJSONObject( i );
                            Log.e("website", website.toString());
                        }
java android android-json
2个回答
1
投票

试试这段代码

 try {
        JSONObject jsonObject = new JSONObject(json);
        JSONArray websites = jsonObject.getJSONArray("website");
        for (int i = 0; i < websites.length(); i++) {
            Log.d("TAG", websites.getString(i));
        }

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

1
投票
// for email array and response variable is your jsonObject coming from server
JSONArray jsonArray = response.getJSONArray("email")
for(int i = 0; i < jsonArray.length(); i++){
   //get email one by one 
   String email = jsonArray.getString(i)
}

如果有效,请告诉我。

© www.soinside.com 2019 - 2024. All rights reserved.