JSONObject to JSONArray FAULT in .getJSONArray( "message" );



我正在尝试将JSONObject转换为JSONArray。我找到了很多例子,尝试了很多东西,但没有任何效果。

所以现在我尝试通过 POST 将 JSON 编码的数组发送到 PHP 脚本。 但是,如果我尝试将 JSONObject 转换为 JSONArray,它会生成错误。演员阵容看起来像:

JSONArray jsonArr = jsonParent.getJSONArray("message");

日志猫输出:

08-17 19:00:32.324:E/短信接收器(17846(:异常 smsReceiverorg.json.JSONException: 没有消息值

以下是所有代码,可为您提供有关我想要执行的操作的上下文:

// Generating the Objects
JSONObject jsonChild= new JSONObject();
JSONObject jsonParent= new JSONObject();
try {       
    jsonChild.put("with_img", false);
    jsonChild.put("message", message);
    jsonChild.put("img", ""); // base64 codiertes Bild
    jsonChild.put("number", senderNum);
    jsonChild.put("time", time); // UNIX Timestamp
} catch (JSONException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
// here enter code here expire the problem
JSONArray jsonArr  = jsonParent.getJSONArray("message");
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://example.com/ticker.php");
StringEntity se = new StringEntity(jsonArr.toString());
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httppost.addHeader("Accept", "application/json");
httppost.addHeader("Content-type", "application/json; charset=UTF-8");
httppost.setEntity(se);
HttpEntity entitySend = httppost.getEntity();
String htmlSend = EntityUtils.toString(entitySend);
Log.d("Sended:", ""+ htmlSend);
try {
    HttpResponse response = httpclient.execute(httppost);
    // Just for reading the server response.
    HttpEntity entity = response.getEntity();
    String htmlResponse = EntityUtils.toString(entity);
    Log.d("Received:", ""+ htmlResponse);
} catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
} catch (IOException e) {
    // TODO Auto-generated catch block
}
我相信

,你想做的是这样的:

// Generating one Object
JSONObject jsonThingy = new JSONObject();
try {       
    jsonThingy.put("with_img", false);
    jsonThingy.put("message", message);
    jsonThingy.put("img", ""); // base64 codiertes Bild
    jsonThingy.put("number", senderNum);
    jsonThingy.put("time", time); // UNIX Timestamp

} catch (JSONException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
StringEntity se = new StringEntity(jsonThingy.toString());
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://example.com/ticker.php");
try {
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("message", se));
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    // Execute HTTP Post Request
    HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
} catch (IOException e) {
    // TODO Auto-generated catch block
}

您的jsonParent对象根本不包含名为 message 的值。如果需要更多帮助,请粘贴 json 数据的内容。

所以现在我尝试通过 POST 将 JSON 编码的数组发送到 PHP 脚本。但是,如果我尝试将 JSONObject 转换为 JSONArray,它会生成错误。演员阵容看起来像:

JSONArray jsonArr = jsonParent.getJSONArray("message"(;

你不是把你的JSONObject投向那条线的JSONArray。您正在尝试获取一个名为"message"的数组jsonParent该数组。基本上,如果你的 JSONObject 包含:

{
    ...
    "message" : [
        {
           "attr1" : "value1",
           "attr2" : "value2",
           ...
        },
        {
           ...
        }
        ...
    ]
    ...
}

它们jsonArr将为您返回该消息数组。

如果要将JSONObject添加到JSONArray,他们会创建一个JSONArray并将其添加到其中。

JSONArray jsonArray = new JSONArray();
jsonArray.put(jsonChild);

最新更新