jsonArray.toString()给了我不完整的json

时间:2013-09-16 08:26:06

标签: android json

我正在检索Android手机的联系人并创建它的JSONArray,光标计数给了我所有联系人和JSONArray的长度,其中我将所有联系人都放在一起,这意味着所有联系人都已成功放入JSONArray。但每当我试图在logcat中打印JsonArray.toString()时,我都会得到不完整的Json,即。它介于两者之间?避免这种情况的解决方案是什么? 提前致谢

正在检索联系人并放入json

的代码
Uri URI = ContactsContract.Contacts.CONTENT_URI;
        String[] projection = { ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME,
                ContactsContract.Contacts.HAS_PHONE_NUMBER };
        String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER;

        Cursor contactCursor = c.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null,
                   ContactsContract.Contacts.HAS_PHONE_NUMBER + " = 1", 
                   null, 
                   "UPPER(" + ContactsContract.Contacts.DISPLAY_NAME + ") ASC");
        Log.d("Alpha","Cursor count  is "+contactCursor.getCount());
         jsonArray=new JSONArray();
        int count=0;
         while(contactCursor.moveToNext())
         {
                String id = contactCursor.getString(contactCursor.getColumnIndex(BaseColumns._ID));
                String name = contactCursor.getString(contactCursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                String phone = "";
                if (Integer
                        .parseInt(contactCursor.getString(contactCursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
                {
                    Cursor pCur = c.getContentResolver().query(Phone.CONTENT_URI, null, Phone.CONTACT_ID + " = ?", new String[] { id },
                            null);

                    while (pCur.moveToNext())
                    {
                        JSONObject json = new JSONObject();
                        phone = pCur.getString(pCur.getColumnIndex(Phone.NUMBER));
                        phone = phone.replace("(", "");
                        phone = phone.replace(")", "");
                        phone = phone.replace("-", "");
                        phone = phone.replace(" ", "");

                        try
                        {
                            json.put("Name", name);
                            json.put("Number", phone);
                            Log.d("Alpha",name + " "+phone+" "+count);
                        }
                        catch (JSONException e)
                        {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

                        jsonArray.put(json);

                    }

                }
                count++;


         }
         Log.d("Alpha","populated jsonArray is "+jsonArray.length());
         String dataToBeSend=jsonArray.toString();
         Log.d("Alpha","populated jsonArray to string is "+dataToBeSend);
        return null;
    }

1 个答案:

答案 0 :(得分:2)

参考Thread "displaying-more-string-on-logcat",我认为您的json字符串长度太长(> 4000个字符)。