字符串org.json.JSONException:字符处未终止的对象

时间:2018-10-28 18:36:25

标签: java android json http

我正在尝试对json进行http调用,该json很长。

尝试执行此操作时:

 JSONObject json = new JSONObject(htmlString);

错误:

 at org.json.JSONTokener.syntaxError(JSONTokener.java:449)
        at org.json.JSONTokener.readObject(JSONTokener.java:393)
        at org.json.JSONTokener.nextValue(JSONTokener.java:100)
        at org.json.JSONTokener.readArray(JSONTokener.java:429)
        at org.json.JSONTokener.nextValue(JSONTokener.java:103)
W/System.err:     at org.json.JSONTokener.readObject(JSONTokener.java:384)
        at org.json.JSONTokener.nextValue(JSONTokener.java:100)
        at org.json.JSONTokener.readObject(JSONTokener.java:384)
        at org.json.JSONTokener.nextValue(JSONTokener.java:100)
        at org.json.JSONTokener.readArray(JSONTokener.java:429)
        at org.json.JSONTokener.nextValue(JSONTokener.java:103)
        at org.json.JSONTokener.readObject(JSONTokener.java:384)
        at org.json.JSONTokener.nextValue(JSONTokener.java:100)
        at org.json.JSONTokener.readObject(JSONTokener.java:384)
        at org.json.JSONTokener.nextValue(JSONTokener.java:100)
        at org.json.JSONObject.<init>(JSONObject.java:156)
        at org.json.JSONObject.<init>(JSONObject.java:173)
         .......

代码:

 private class Html extends AsyncTask<Void, Void, Void> {
            private String url = "https://pastebin.com/raw/2T93TvDU";
            private JSONArray array = new JSONArray();

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
            }

            @Override
            protected Void doInBackground(Void... params) {
                try {
                    Log.v("Class:" + TAG, "doInBackground:" + url);

                    Document doc = Jsoup.connect(url).get();
                    String htmlString = doc.toString();
                    htmlString = htmlString.replaceAll("<html>", "");
                    htmlString = htmlString.replaceAll("</html>", "");
                    htmlString = htmlString.replaceAll("<head>", "");
                    htmlString = htmlString.replaceAll("</head>", "");
                    htmlString = htmlString.replaceAll("<body>", "");
                    htmlString = htmlString.replaceAll("</body>", "");


                    try {
                        Log.v("Class:" + TAG, "Js:" + htmlString);
                        JSONObject json = new JSONObject(htmlString);
                        //Object list = json.get("list");
                        Log.v("Class:" + TAG, "Json:" + json);

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


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

            @Override
            protected void onPostExecute(Void result) {
                populateListItem(array);
            }


        }

1 个答案:

答案 0 :(得分:1)

该URL上的数据不是HTML,而是纯JSON(发送为import React, { Component } from 'react'; import './style.css'; const list = [ { id : 0, title : "Went well", showCard : false, addCard : (func0,func1,func2,func3,func4,color,value)=> { let id = "x"+count++ let myProps = {id:id,deleteCard:func0,handleChange:func1,moveRight:func2,moveLeft:func3,setText:func4,key:id,color:color,value:value} return <Card {...myProps}/>}, cards : [], color : "pink" }, { id : 1, title : "To Improve", showCard : false, addCard : (func0,func1,func2,func3,func4,color,value)=> { let id = "y"+count++ let myProps = {id:id,deleteCard:func0,handleChange:func1,moveRight:func2,moveLeft:func3,setText:func4,key:id,color:color,value:value} return <Card {...myProps}/>}, cards : [], color : "yellow" }, { id : 2, title : "Action Items", showCard : false, addCard : (func0,func1,func2,func3,func4,color,value)=> { let id = "z"+count++ let myProps = {id:id,deleteCard:func0,handleChange:func1,moveRight:func2,moveLeft:func3,setText:func4,key:id,color:color,value:value} return <Card {...myProps}/>}, cards : [], color : "blue" } ] let count = 0 class App extends Component { constructor(){ super() this.state = {list : list, value: "", current: "",} this.buttonClick = this.buttonClick.bind(this) this.deleteCard = this.deleteCard.bind(this) this.handleChange = this.handleChange.bind(this) this.moveRight = this.moveRight.bind(this) this.moveLeft = this.moveLeft.bind(this) this.setText = this.setText.bind(this) } buttonClick(ev,id,func0,func1,func2,func3,func4){ let a0 = null this.setState({current:""}) for(let obj of this.state.list){ if(obj.id === id){ console.log("here af") a0 = obj.addCard(func0,func1,func2,func3,func4,obj.color,this.state.value) obj.cards.push(a0) } } this.setState({list:this.state.list}) } moveLeft(ev,id,func0,func1,func2,func3,func4){ ev.preventDefault() let a0 = null if(this.state.value === this.state.current) this.setState({value:this.state.current}) let updatedList = this.state.list.map((obj)=>{ let found = obj.cards.findIndex((element)=> element.key === id) if(obj.id === 0){ if(found !== -1){ obj.cards.splice(found,1) a0 = this.state.list[2].addCard(func0,func1,func2,func3,func4,this.state.list[2].color,this.state.value) this.state.list[2].cards.push(a0) } } if(obj.id === 1){ if(found !== -1){ obj.cards.splice(found,1) a0 = this.state.list[0].addCard(func0,func1,func2,func3,func4,this.state.list[0].color,this.state.value) this.state.list[0].cards.push(a0) } } if(obj.id === 2){ if(found !== -1){ obj.cards.splice(found,1) a0 = this.state.list[1].addCard(func0,func1,func2,func3,func4,this.state.list[1].color,this.state.value) this.state.list[1].cards.push(a0) } } return obj }) this.setState({list:updatedList,}) } moveRight(ev,id,func0,func1,func2,func3,func4){ ev.preventDefault() let a0 = null let updatedList = this.state.list.map((obj)=>{ let found = obj.cards.findIndex((element)=> element.key === id) if(obj.id === 0){ if(found !== -1){ obj.cards.splice(found,1) a0 = this.state.list[1].addCard(func0,func1,func2,func3,func4,this.state.list[1].color,this.state.value) this.state.list[1].cards.push(a0) } } if(obj.id === 1){ if(found !== -1){ obj.cards.splice(found,1) a0 = this.state.list[2].addCard(func0,func1,func2,func3,func4,this.state.list[2].color,this.state.value) this.state.list[2].cards.push(a0) } } if(obj.id === 2){ if(found !== -1){ obj.cards.splice(found,1) a0 = this.state.list[0].addCard(func0,func1,func2,func3,func4,this.state.list[0].color,this.state.value) this.state.list[0].cards.push(a0) } } return obj }) this.setState({list:updatedList,}) } setText(ev){ this.setState({value:ev.target.value,current:ev.target.value}) } deleteCard(ev,id){ ev.preventDefault() let updatedList = this.state.list.map((obj)=>{ let found = obj.cards.findIndex((element)=> element.key === id) if(found !== -1) obj.cards.splice(found,1) return obj }) this.setState({list:updatedList}) } handleChange(ev){ this.setState({value:ev.target.value,current:ev.target.value}) } render() { return ( <div className="App"> <h2>Retro Board</h2> <ul className="container"> {this.state.list.map((item) => <Contak key={item.title+item.id} text={item.title} buttonClick={this.buttonClick} deleteCard={this.deleteCard} handleChange={this.handleChange} moveRight={this.moveRight} moveLeft={this.moveLeft} setText={this.setText} showCard={item.showCard} id={item.id} cards={item.cards} /> )} </ul> </div> ) } } function Contak(props){ return <li> <h3>{props.text}</h3> <ul className="stack"> <li><button id={props.text} type="button" className="block" onClick={e =>props.buttonClick(e,props.id,props.deleteCard,props.handleChange,props.moveRight,props.moveLeft,props.setText)}>+</button></li> {props.cards.map((card)=> { console.log("card",card) return card || null })} </ul> </li> } function Card(props){ return <li> <div className="card" style={{backgroundColor: props.color}}> <textarea type="text" className="card" placeholder="Enter text here" defaultValue={props.value} onChange={e =>props.handleChange(e)} onBlur={e => props.setText(e)}></textarea> <div><a className="ltCtl" href="./logo" onClick={e=>props.moveLeft(e,props.id,props.deleteCard,props.handleChange,props.moveRight,props.moveLeft,props.setText)}>&lt;</a><a className="clCtl" href="./logo" onClick={e =>props.deleteCard(e,props.id)}>x</a><a className="rtCtl" href="./logo" onClick={e =>props.moveRight(e,props.id,props.deleteCard,props.handleChange,props.moveRight,props.moveLeft,props.setText)}>&gt;</a></div> </div> </li> } export default App; )。使用text/plain会尝试将其转换为HTML文档,对数据进行错误的编码和解码,并返回错误的结果。而是使用connect().get()直接获取JSON字符串。

connect().execute().body()