简体   繁体   English

JSONArray.toString()在Java中不起作用

[英]JSONArray.toString() not working in Java

I try to send data from my android client to my web server, using JSON. 我尝试使用JSON将数据从Android客户端发送到Web服务器。

I am facing an issue. 我面临一个问题。 Since now more than two days of debugging, it seems that the problem the toString method of the JSONArray class is not working properly. 从现在开始超过两天的调试以来,似乎JSONArray类的toString方法问题无法正常工作。

I have the constraint that I cannot use the GSON library. 我有不能使用GSON库的约束。 Thus, I am dealing only with the JSONArray and JSONObject libraries. 因此,我只处理JSONArray和JSONObject库。

I construct my JSONArray as follows: 我按如下方式构造JSONArray:

ArrayList<SMS> SMSArray = SMSHelper.getSMSDetails(context, rEmail, rPhoneNum);
JSONArray jSMSArray = new JSONArray();

for(int i = 0; i < SMSArray.size(); i++) {
    jSMSArray.put(i, SMSArray.get(i).toJSON());
    Log.wtf("::trace", "JSONArray.get(i).toString(): " + jSMSArray.get(i).toString());
}

Log.wtf("::trace", "JSONArray.toString(): " + jSMSArray.toString()); Log.wtf(“ :: trace”,“ JSONArray.toString():” + jSMSArray.toString());

Inside of the loop, I get the exact same string printed out from my JSONArray and my ArrayList. 在循环内部,我从JSONArray和ArrayList中打印出了完全相同的字符串。 SO it seems that the copy of the ArrayList into JSONArray works fine. 因此,似乎将ArrayList复制到JSONArray可以正常工作。 Plus after the loop the size of the two Array are the same. 再加上循环后,两个数组的大小是相同的。

Nevertheless, when I use JSONArray.toString() after the loop, surprisingly, the string is not complete the string prints only few JSONObject out of 285 JSONObject that contains my JSONArray! 不过,当我在循环后使用JSONArray.toString()时,令人惊讶的是,字符串未完成,在包含我的JSONArray的285个JSONObject中,该字符串仅打印了少数JSONObject!

I want to send this JSONARRAY to my server to parse it on the server side but as the JSONArray is not correct. 我想将此JSONARRAY发送到我的服务器以在服务器端对其进行解析,但由于JSONArray不正确。 The server cannot parse the received string chain. 服务器无法解析收到的字符串链。 The received String chain looks like: [{object1}, ..., {objecti}, {"id", $myId, "thread", $myThread, "content", "hey 收到的字符串链看起来像:[{object1},...,{objecti},{“ id”,$ myId,“ thread”,$ myThread,“ content”,“嘿

I really hope that one of you will be able to help me this. 我真的希望你们中的一个能够帮助我。 It is driving me crazy; 它使我发疯。 first I used the syntax for(Object object: ObjectArray) but then I learnt that a JSONArray is not an iterable object so I tried to direcly put the JSONObject inside a specific index but I got the same outcome. 首先,我for(Object object: ObjectArray)使用了语法for(Object object: ObjectArray)但随后我了解到JSONArray不是可迭代的对象,因此我尝试将JSONObject直接放入特定的索引中,但得到的结果相同。

This code is part of my Master Thesis, and I am loosing a good amount of time on this details... :/ 这段代码是我的硕士论文的一部分,我在这些细节上花了很多时间...:/

I really, look forward to your answers. 我真的很期待您的回答。

Goodnight, 晚安,

Björn 比约恩

EDIT: My JSONObject are the SMS stored in the android mobile phone. 编辑:我的JSONObject是存储在android手机中的SMS。 My fonction to convert them into JSONObject is the following: 我将它们转换为JSONObject的功能如下:

public JSONObject toJSON() {
    JSONObject jSMS = new JSONObject();
    try {
        jSMS.put("id", this.id);
        jSMS.put("thread", this.thread);
        jSMS.put("rEmail", this.rEmail);
        jSMS.put("rPhoneNum", this.rPhoneNum);
        jSMS.put("sPhoneNum", this.sPhoneNum);
        jSMS.put("sName", this.sName);
        jSMS.put("date", this.date);
        jSMS.put("content", this.content);
        jSMS.put("type", this.type);
    } catch (JSONException e) {
        Log.wtf("::trace", Log.getStackTraceString(e));
    }
    return jSMS;
}

EDIT1: When I loop through my JSONArray and use JSONArray.getJSONObject(i).toString() everything works fine it prints me the whole content of the Array, when I call the JSONArray.toString() method it prints me out only the beginning of the JSONArray. EDIT1:当我遍历我的JSONArray并使用JSONArray.getJSONObject(i).toString()一切正常,它将向我显示数组的全部内容,当我调用JSONArray.toString()方法时,它仅向我开头JSONArray。 I really start to be tired by that. 我真的开始对此感到厌倦。 I have no idea what can causes such error. 我不知道什么会导致这种错误。

see What is the size limit for Logcat and how to change its capacity? 请参阅Logcat的大小限制以及如何更改其容量?

Every logcat entry is limited to a certain number of characters (depending on your android device). 每个logcat条目都限制为一定数量的字符(取决于您的android设备)。 It is very possible that a JSON array 285 objects long is too long for a single logcat entry, so it just cuts it in the middle. 对于单个logcat条目而言,很可能JSON对象285个对象的长度太长,因此它只在中间切入。 I don't think there is a problem with your json objects (or if there is, it's not the one you found) 我认为您的json对象没有问题(或者如果存在,那不是您找到的对象)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM