【问题标题】:JSONObject to JSONArray FAULT in .getJSONArray("message");JSONObject to JSONArray FAULT in .getJSONArray("message");
【发布时间】:2014-08-17 17:07:46
【问题描述】:

我正在尝试将 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
}

【问题讨论】:

    标签: android json


    【解决方案1】:

    我相信,你想做的是这样的:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

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

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

        在该行中,您没有将 JSONObject 转换为 JSONArray。您正在尝试获取位于jsonParent 内的名为“消息”的数组。基本上,如果您的 JSONObject 包含:

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

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

        如果您想将您的 JSONObject 添加到 JSONArray,他们创建一个 JSONArray 并将其添加到其中。

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-12-19
          • 1970-01-01
          • 2016-10-28
          • 2018-07-18
          相关资源
          最近更新 更多