Android org.json.JSONException:无法将java.lang.String类型的值结果转换为JSONObject

Android org.json.JSONException:无法将java.lang.String类型的值结果转换为JSONObject,android,json,android-asynctask,Android,Json,Android Asynctask,获取org.json.JSONException:类型为java.lang.String的值结果无法转换为JSONObject。代码哪里出错了?这是一个从实时流媒体获取值的程序。完整代码如下所示。。 给定的JSON race: { id: "44708", track_id: "1", track: "International", starts_at: "2016-05-04 06:16:00", finish_time: "1970-01-01 01:00:00"

获取
org.json.JSONException
:类型为
java.lang.String
的值结果无法转换为
JSONObject
。代码哪里出错了?这是一个从实时流媒体获取值的程序。完整代码如下所示。。 给定的JSON

    race: {
  id: "44708",
  track_id: "1",
  track: "International",
  starts_at: "2016-05-04 06:16:00",
  finish_time: "1970-01-01 01:00:00",
  heat_type_id: "123",
  heat_status_id: "1",
  speed_level_id: "5",
  speed_level: "Nat Cadet & Junior",
  win_by: "position",
  race_by: "minutes",
  duration: 10,
  race_name: "Inkart National Heat",
  race_time_in_seconds: 276.336
},
scoreboard: [
  {
    position: "1",
    nickname: "Emily Linscott",
    average_lap_time: "90.053",
    fastest_lap_time: "60.490",
    last_lap_time: "60.490",
    rpm: "1225",
    first_name: "Emily",
    last_name: "Linscott",
    is_first_time: "0",
    total_races: "6",
    racer_id: "1157509",
    lap_num: "3",
    kart_num: "63",
    gap: ".000",
    ambtime: "31728819591"
  },
代码

受保护的JSONObject doInBackground(字符串…URL){
JSONObject jsonObj=null;
Log.d(“标签”、“内部背景”);
试一试{
URL=新URL(“http://daytonamk.clubspeedtiming.com/api/index.php/races/scoreboard.json?track_id=1&key=cs-dev“+kartNumber);
HttpURLConnection urlConnection=(HttpURLConnection)url.openConnection();
urlConnection.setRequestMethod(“GET”);
BufferedInputStream inputStream=新的BufferedInputStream(urlConnection.getInputStream());
字符串结果=convertInputStreamToString(inputStream);
解析结果(result);
Log.e(“消息”,“这是一条消息”);
}catch(filenotfounde异常){
e、 printStackTrace();
}捕获(例外e){
e、 printStackTrace();
}
返回jsonObj;
}
私有字符串convertInputStreamToString(InputStream InputStream)引发IOException{
BufferedReader BufferedReader=新的BufferedReader(新的InputStreamReader(inputStream));
StringBuilder=新的StringBuilder();
弦线;
字符串结果=”;
{
而((line=bufferedReader.readLine())!=null){
builder.append(行);
}
返回结果;
}
}
私有JSONObject parseResult(字符串结果){
JSONObject jsonObj=null;
如果(结果==null){
Toast.makeText(Main_Activity.this,“当前未运行的比赛”,Toast.LENGTH_LONG.show();
}否则{
试一试{
整数=0;
jsonObj=新的JSONObject(“结果”);
JSONObject race=jsonObj.getJSONObject(“race”);
int durationinmin=race.getInt(“持续时间”);
String win_by=race.getString(“win_by”);
JSONArray记分板=jsonObj.getJSONArray(“记分板”);
Log.d(“标记”,“Json值:”+记分板);
对于(int i=0;i
正如您的logcat读取的
org.json.JSONException:java.lang.String类型的值结果无法转换为JSONObject

记录您得到的响应,而不首先解析数据。然后您可以了解您正在处理的数据类型,例如:
字符串、整数、浮点、

在这里发布回复以获得更多帮助

编辑: 这里的这一行
jsonObj=newjsonobject(“结果”);
改为这样做
jsonObj=newjsonobject(result);

编辑:

jsonObj=新的JSONObject(结果);
JSONObject obj=jsonObj.getJSONObject(“race”);
字符串id=obj.getString(“id”);

//对race中的所有对象执行相同的操作
请发布json和logcat消息我更新了json和logcat..现在尝试以正确的格式发布..感谢Praful Bhatnagar..如您所说更改..但获得相同的异常..将记录响应并更新包含的整个json?如{race{..}或[race{..}
protected JSONObject doInBackground(String... urls) {
        JSONObject jsonObj = null;
        Log.d("TAG","inside doInBackground..");
        try {
            URL url = new URL("http://daytonamk.clubspeedtiming.com/api/index.php/races/scoreboard.json?track_id=1&key=cs-dev" + kartNumber);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("GET");


            BufferedInputStream inputStream = new BufferedInputStream(urlConnection.getInputStream());
            String result = convertInputStreamToString(inputStream);
            parseResult(result);

            Log.e("Message", "This is a message");

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

        return jsonObj;
    }

    private String convertInputStreamToString(InputStream inputStream) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        StringBuilder builder = new StringBuilder();
        String line;
        String result = "";

        {
            while ((line = bufferedReader.readLine()) != null) {
                builder.append( line);
            }




            return result;

        }

    }


    private JSONObject parseResult(String result) {
        JSONObject jsonObj = null;
        if (result == null) {
            Toast.makeText(Main_Activity.this, "Race not currently running", Toast.LENGTH_LONG).show();
        } else {
            try {
                int number = 0;


                jsonObj = new JSONObject("result");

                JSONObject race = jsonObj.getJSONObject("race");
                int durationInMins = race.getInt("duration");
                String win_by = race.getString("win_by");

                JSONArray scoreboard = jsonObj.getJSONArray("scoreboard");
                Log.d("TAG", "Json value :" + scoreboard);
                for (int i = 0; i < scoreboard.length(); i++) {
                    JSONObject data = scoreboard.getJSONObject(i);


                    number = data.getInt("kart_num");


                    while (kartNumber == number) {
                        int gridPosition = data.getInt("position");
                        int gap = data.getInt("gap");
                        int lastLapTime = data.getInt("last_lap_time");
                        int bestLapTime = data.getInt("fastest_lap_time");


                        int raceInTime = race.getInt("race_time_in_seconds");

                        duration = durationInMins * 60 - raceInTime;


                        if (!(gridPosition == 1)) {
                            JSONObject dataPreviousGap = scoreboard.getJSONObject(i - 1);
                            int gapPrev = dataPreviousGap.getInt("gap");
                            gapUp = gap - gapPrev;
                        }
                        if (!(i == scoreboard.length() - 1)) {
                            JSONObject dataNext = scoreboard.getJSONObject(i + 1);
                            int gapNext = dataNext.getInt("gap");
                            gapDown = gapNext - gap;
                        }
                    }
                }


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


    protected void onPostExecute(JSONObject jsonObj) {

        Intent myIntent = new Intent(Main_Activity.this, Display_Activity.class);

        try {
           JSONObject jsonObje = new JSONObject("result");

        JSONArray scoreboard = jsonObje.getJSONArray("scoreboard");
        for (int i = 0; i < scoreboard.length(); i++) {
        JSONObject data = scoreboard.getJSONObject(i);
        myIntent.putExtra("GridPosition", data.getInt("position"));
        myIntent.putExtra("LastLapTime", data.getInt("last_lap_time"));
        myIntent.putExtra("MinutesToGo", duration);
        myIntent.putExtra("BestLapTime",data.getInt("fastest_lap_time"));
        myIntent.putExtra("GapUp", gapUp);
        myIntent.putExtra("GapDown", gapDown);

        }