从这个页面,
org.json.JSONException:字符 550 处的预期文字值
从我的 asp.net 网络服务我得到
"[{\"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,最后我可以使用它了。我确信这是最糟糕的方式。我也确信这一定是我的网络服务发送数据的方式。有什么更令人愉快的方式来做到这一点?
结果是:
"[{"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;
}