从此页面
org.json.JSONException: Expected literal value at character 550 of
从我的asp.net webservice获取
"[{\"ID\":1,\"Title\":\"When Harry Met Sally\",\"ReleaseDate\":\"1989-01-11T00:00:00\",\"Genre\":\"Romantic Comedy\",\"Price\":7.99,\"Rating\":null},{\"ID\":2,\"Title\":\"Ghostbusters \",\"ReleaseDate\":\"1984-03-13T00:00:00\",\"Genre\":\"Comedy\",\"Price\":8.99,\"Rating\":null},{\"ID\":3,\"Title\":\"Ghostbusters 2\",\"ReleaseDate\":\"1986-02-23T00:00:00\",\"Genre\":\"Comedy\",\"Price\":9.99,\"Rating\":null},{\"ID\":4,\"Title\":\"Rio Bravo\",\"ReleaseDate\":\"1959-04-15T00:00:00\",\"Genre\":\"Western\",\"Price\":3.99,\"Rating\":null},{\"ID\":5,\"Title\":\"Gone With The wind\",\"ReleaseDate\":\"1981-01-01T00:00:00\",\"Genre\":\"Hippys\",\"Price\":10.00,\"Rating\":\"Ok\"},{\"ID\":6,\"Title\":\"Happy Hour\",\"ReleaseDate\":\"1911-01-01T00:00:00\",\"Genre\":\"Supers\",\"Price\":33.00,\"Rating\":\"Good\"},{\"ID\":7,\"Title\":\"Happy Hour\",\"ReleaseDate\":\"1911-01-01T00:00:00\",\"Genre\":\"Supers\",\"Price\":33.00,\"Rating\":\"Good\"}]"
我收到了:
"[{\"ID\":1,\"Title\":\"When Harry Met Sally\",\"ReleaseDate\":\"1989-01-11T00:00:00\",\"Genre\":\"Romantic Comedy\",\"Price\":7.99,\"Rating\":null},{\"ID\":2,\"Title\":\"Ghostbusters \",\"ReleaseDate\":\"1984-03-13T00:00:00\",\"Genre\":\"Comedy\",\"Price\":8.99,\"Rating\":null},{\"ID\":3,\"Title\":\"Ghostbusters 2\",\"ReleaseDate\":\"1986-02-23T00:00:00\",\"Genre\":\"Comedy\",\"Price\":9.99,\"Rating\":null},{\"ID\":4,\"Title\":\"Rio Bravo\",\"ReleaseDate\":\"1959-04-15T00:00:00\",\"Genre\":\"Western\",\"Price\":3.99,\"Rating\":null},{\"ID\":5,\"Title\":\"Gone With the Wind\",\"ReleaseDate\":\"1990-01-01T00:00:00\",\"Genre\":\"hippys\",\"Price\":40.00,\"Rating\":\"ok\"},{\"ID\":6,\"Title\":\"Scary\",\"ReleaseDate\":\"1990-01-01T00:00:00\",\"Genre\":\"Hippys\",\"Price\":30.00,\"Rating\":\"Ok\"},{\"ID\":7,\"Title\":\"Fox Trot\",\"ReleaseDate\":\"1960-04-04T00:00:00\",\"Genre\":\"Gold\",\"Price\":20.00,\"Rating\":\"Magic\"},{\"ID\":8,\"Title\":\"Happy Hour\",\"ReleaseDate\":\"1911-01-01T00:00:00\",\"Genre\":\"Supers\",\"Price\":33.00,\"Rating\":\"Good\"}]"
然后我使用上面的代码,在上面的页面上,编辑字符串以删除斜杠,引号和n,然后最后我可以使用它。我确信这是最糟糕的方式。我也确定它必须是我的Web服务发送数据的方式。这样做有什么更令人愉快的方式?
结果是:
"[{"ID":1,"Title":"When Harry Met Sally","ReleaseDate":"1989-01-11T00:00:00","Genre":"Romantic Comedy","Price":7.99,"Rating":null},{"ID":2,"Title":"Ghostbusters ","ReleaseDate":"1984-03-13T00:00:00","Genre":"Comedy","Price":8.99,"Rating":null},{"ID":3,"Title":"Ghostbusters 2","ReleaseDate":"1986-02-23T00:00:00","Genre":"Comedy","Price":9.99,"Rating":null},{"ID":4,"Title":"Rio Bravo","ReleaseDate":"1959-04-15T00:00:00","Genre":"Western","Price":3.99,"Rating":null},{"ID":5,"Title":"Gone With the Wind","ReleaseDate":"1990-01-01T00:00:00","Genre":"hippys","Price":40.00,"Rating":"ok"},{"ID":6,"Title":"Scary","ReleaseDate":"1990-01-01T00:00:00","Genre":"Hippys","Price":30.00,"Rating":"Ok"},{"ID":7,"Title":"Fox Trot","ReleaseDate":"1960-04-04T00:00:00","Genre":"Gold","Price":20.00,"Rating":"Magic"},{"ID":8,"Title":"Happy Hour","ReleaseDate":"1911-01-01T00:00:00","Genre":"Supers","Price":33.00,"Rating":"Good"}]"
真正的JSON。
以下是我正在使用的代码段。
我在C#中的Web服务
private MovieDBContext db = new MovieDBContext();
// Localhost/api/MovieApi/
// GET api/<controller>
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string Get()
{
var GenreList = new List<string>();
var GenQry = from d in db.Movies
orderby d.Genre
select d.Genre;
GenreList.AddRange(GenQry.Distinct());
var movies = from m in db.Movies
select m;
string json = JsonConvert.SerializeObject(movies);
return json;
}
和我的Android将其转换为可用的json,并转换为JSON数组
public String convertStandardJSONString(String data_json){
data_json = data_json.replace("\\", "");
data_json = data_json.replace("\"{", "{");
data_json = data_json.replace("}\",", "},");
data_json = data_json.replace("}\"", "}");
return data_json;
}
public List<Movie> findAllItems(String movies) {
movies = convertStandardJSONString(movies);
movies = movies.substring(1, movies.length()-1);
JSONArray jArray = null;
try {
jArray = new JSONArray(movies);
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
...
和异步任务
public String requestWebService(String serviceUrl) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
//HttpPost httpPost = new HttpPost(serviceUrl);
HttpGet httpGet = new HttpGet(serviceUrl);
HttpResponse httpResponse = httpClient.execute(httpGet);//);Post);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
// return JSON String
return json;
}
答案 0 :(得分:1)
我认为这条线看起来有点可疑:
while ((line = reader.readLine()) != null) {
sb.append(line + "n");
}
希望能帮助您进行调试。