How to convert single string to JsonArray in android?

前端 未结 2 474
时光说笑
时光说笑 2021-01-25 00:08

I need to convert a String[] to an JsonArray and I don\'t know how. I am new in android development i want to insert call log details in MySQL database. so, from android side i

相关标签:
2条回答
  • 2021-01-25 00:35

    Try this,

    // Create JSONArray
    JSONArray jArray = new JSONArray();
    while (managedCursor.moveToNext())
    {
        final String number = managedCursor.getString(number1);
        final String type2 = managedCursor.getString(type1);
        final String date = managedCursor.getString(managedCursor.getColumnIndexOrThrow("date")).toString();
        Date date1 = new Date(Long.valueOf(date));
        final String fDate = date1.toString();
        final String duration = managedCursor.getString(duration1);
        String type = null;
    
        // Create JSONObject
        JSONObject item = new JSONObject();
    
        // add the items to JSONObject
        item.put("number", number);
        item.put("type2", type2);
        item.put("fDate", fDate);
        item.put("duration", duration);
    
        // add the JSONObject to JSONArray
        jArray.put(item);
    }
    managedCursor.close();
    
    System.out.println(jArray.toString());
    
    0 讨论(0)
  • 2021-01-25 00:42

    It is easy.You can use one of the constructors of JSONArray class.

    JSONArray jArr = new JSONArray(strArr)
    

    where strArr is your String array.

    More here: https://developer.android.com/reference/org/json/JSONArray.html#JSONArray(java.lang.Object)

    0 讨论(0)
提交回复
热议问题