值类型java.lang.String的值无法转换为JSONArray

时间:2017-11-09 07:51:43

标签: java android json sqlite

我正在尝试将#!/bin/sh # handling command-line input file=$1 ecf=$2 # iterating through file and pulling out # information for the GET- and POST-request while read -r line do parent=$(echo $line | awk '{print substr($1,2,3)}') start=$(echo $line | awk '{print substr($2,2,6)}') end=$(echo $line | awk '{print substr($3,2,6)}') strand=$(echo $line | awk '{print substr($4,2,1)}') locus=$(echo $line | awk '{print substr($6,2,8)}') # depending on $parent, the right insertion for the URL is generated if [ $parent = "SMc" ] ; then location="Genome" ab="SMc" elif [ $parent = "SMa" ] ; then location="PrintPsyma" ab="pSymA" else [ $parent = "SMb" ] location="PrintPsymb" ab="pSymB" fi # building variables for curl content request options=( --compressed) headers=( -H 'Host: iant.toulouse.inra.fr' -H 'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:56.0) Gecko/20100101 Firefox/56.0' -H 'Accept: txt/html,application/xhtml+xml,application/xml;1=0.9,*/*;q=0.8' -H 'Accept-Language: de,en-US;q=0.7,en;q=0.3' -H 'Referer: https://iant.toulouse.inra.fr/bacteria/annotation/cgi/rhime.cgi?__wb_cookie=&__wb_cookie_name=auth.rhime&__wb_cookie_path=/bacteria/annotation/cgi&__wb_session=WB84Qfsf&__wb_main_menu=Genome&__wb_function=$parent' -H 'Content-Type: application/x-www-form-urlencoded' -H 'Connection: keep-alive' -H 'Upgrade-Insecure-Requests: 1' -H 'Pragma: no-cache' -H 'Cache-Control: no-cache' ) url='https://iant.toulouse.inra.fr/bacteria/annotation/cgi/rhime.cgi' ab=$(urlencode "${ab}") start=$(urlencode "${start}") end=$(urlencode "${end}") strand=$(urlencode "${strand}") data="__wb_function=PortalExtractSeq&mode=run&species=rhime&fastafile=%2Fwww%2Fbacteria%2Fannotation%2F%2Fsite%2Fprj%2Frhime%2F%2Fdb%2F$ab.genomic&begin=$start&end=$end&strand=$strand" # storing HTTP response code in variable response. Only if the # reponse code is OK (200), we move on response=$(curl -X HEAD -I --header 'Accept:txt/html' "https://iant.toulouse.inra.fr/bacteria/annotation/cgi/rhime.cgi?__wb_cookie=&__wb_cookie_name=auth.rhime&__wb_cookie_path=/bacteria/annotation/cgi&__wb_session=WB8jqwTM&__wb_main_menu=Genome&__wb_function="$location""|head -n1|awk '{print $2}') echo "$response" # getting information via curl request if [ $response = 200 ] ; then info=$(curl -G "${options[@]}" "${headers[@]}" --data "${data}" "${url}") fi echo $info done < $file 从服务器保存到sqlite,但我收到了错误

json_data

这是我的json:My Json

这是我的java代码:

Value Next of type java.lang.String cannot be converted to JSONArray

我知道这段代码看起来很奇怪,但请帮助我这样做。

5 个答案:

答案 0 :(得分:2)

  

“无法将类型java.lang.String的值下一步转换为   JSONArray”。

纠正 {% load static %} <div class="col-md-4"> <img src= "{% static "images/abc.jpg" %} alt="sorry"/> </div> 部分。

<强>不要

getJSONObject

<强>不要

 JSONObject object1 = new JSONObject("Next");

答案 1 :(得分:2)

您需要提取Next jsonobject,而不是在此处使用String一样创建

new JSONObject("Next");,因此请从响应object中获取

JSONObject object1 = object.getJSONObject("Next");

答案 2 :(得分:0)

JSONObject result_obj= new JSONObject(result.toString());
 JSONObject result_array= result_obj.getJSONObject("Next");
 JSONArray array_values = result_array.getJSONArray("Days");
 for (int i = 0; i < Jarray.length(); i++)
 {
     JSONObject plan = Jarray.getJSONObject(i);
      plan.getString("Day");
      for(int j=0;j<plan.lenght();j++){


       }

 }

答案 3 :(得分:0)

您需要从提供的json中解析“Next”对象。您创建了一个错误的新JSONObject。

  try {
        String json_data = stringBuilder.toString().trim();
        JSONObject object = new JSONObject(json_data);
        JSONObject object1 =object.getJSONObject("Next");
        JSONArray Jarray  = object1.getJSONArray("Days");
        for (int i = 0; i < Jarray.length(); i++)
        {
            JSONObject Jasonobject = Jarray.getJSONObject(i);
            dbHelper.putStationsInformation((Jasonobject),sqLiteDatabase);
            dbHelper.close();

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

快乐的编码!!

答案 4 :(得分:0)

您已经创建了一个新的JSONObject(“Next”),这意味着您将字符串“Next”转换为JSONObject。 纠正这个:

String json_data = stringBuilder.toString().trim();
JSONObject object = new JSONObject(json_data);
JSONObject object1 = object.getJSONObject("Next");