.JSONObject到JSONArray故障在.getJSONArray(“message”);

时间:2014-08-17 17:07:47

标签: android json

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

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

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

LogCat输出:

  

08-17 19:00:32.324:E / SmsReceiver(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
}

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

  

所以现在我尝试通过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);

答案 2 :(得分:0)

我相信,你想要做的是:

// 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
}