我有一个 Java 应用程序通过 PHP 脚本使用 JSON 与 SQL 数据库通信。我之前使用 JSON 对数组进行 json_encode,效果很好,您可以使用每个条目的键调用 Java 中的值。但是现在,我在 PHP 中对一个字符串进行了 json_encoded,如下所示:
json_encode("成功!");
现在,我如何在业务的 Java 端请求该值?我需要将一些东西作为 key 放入 jsonResponse.getString('key');
中。关键是什么...
我希望你能理解我的问题...
public String send(String username, String password, String database){
//Create a HTTPClient as the form container
HttpClient httpclient;
//Use HTTP POST method
HttpPost httppost;
//Create an array list for the input data to be sent
ArrayList<NameValuePair> nameValuePairs;
//Create a HTTP Response and HTTP Entity
HttpResponse response;
HttpEntity entity;
//Create new default HTTPClient
httpclient = new DefaultHttpClient();
//Create new HTTP POST with URL to php file as parameter
httppost = new HttpPost("http://192.168.144.150/login/add.php");
String pass = md5(password);
//Next block of code needs to be surrounded by try/catch block for it to work
try {
//Create new Array List
nameValuePairs = new ArrayList<NameValuePair>();
//place them in an array list
nameValuePairs.add(new BasicNameValuePair("username", username));
nameValuePairs.add(new BasicNameValuePair("password", pass));
nameValuePairs.add(new BasicNameValuePair("database", database));
//Add array list to http post
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
//assign executed form container to response
response = httpclient.execute(httppost);
//check status code, need to check status code 200
if(response.getStatusLine().getStatusCode()== 200){
//assign response entity to http entity
entity = response.getEntity();
//check if entity is not null
if(entity != null){
//Create new input stream with received data assigned
InputStream instream = entity.getContent();
//Create new JSON Object. assign converted data as parameter.
//JSONObject jsonResponse = new JSONObject(convertStreamToString(instream));
JSONArray a = new JSONArray(convertStreamToString(instream));
//assign json responses to local strings
String res = a.getString(0);
//Return the json response to the gui
return res;
} else {
//Toast.makeText(this, "kapot", Toast.LENGTH_SHORT).show();
return new String("De entiteit is leeg. Kortom: kapot");
}
} else {
//Toast.makeText(this, "statuscode was niet 200", Toast.LENGTH_SHORT).show();
return new String("De statuscode was niet 200.");
}
} catch(Exception e){
e.printStackTrace();
//Display toast when there is a connection error
//Change message to something more friendly
//Toast.makeText(this, "verbindingsproblemem", Toast.LENGTH_SHORT).show();
return new String("Er is een fout opgetreden. Controleer a.u.b. uw gegevens en de internetverbinding.");
}
不要介意荷兰语部分,这不重要。
最佳答案
json_encode("Success!")
的输出是 "Success"
,它本身不是有效的 JSON document .有效的 JSON 文档的顶级实体(根)必须是对象或数组。
如果你只想返回一个字符串,你可以这样做:
使其成为数组中的唯一条目:
json_encode(array("Success!"))
...结果是
["Success!"]
,您可以在您的 Java 代码中像这样使用它(因为您说您正在使用org.java
库):JSONArray a = new JSONArray(jsonStringFromPHP); String s = a.getString(0);
或者当然
String s = new JSONArray(jsonStringFromPHP).getString(0);
或者让它成为一个对象的唯一属性:
json_encode(array('result' => "Success!")
...结果是
{"result":"Success!"}
,您可以在 Java 代码中像这样使用它:JSONObject o = new JSONObject(jsonStringFromPHP); String s = o.getString("result");
或者当然
String s = new JSONObject(jsonStringFromPHP).getString("result");
您可能会使用 JSONTokenizer
读取您当前的 JSON 片段(“成功!”
),但我不建议这样做。坚持交换有效的 JSON 文档。
关于java - JSON 编码字符串的键是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13136711/