Skip to content Skip to sidebar Skip to footer

How To Convert Single String To Jsonarray In Android?

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 am g

Solution 1:

Try this,

// Create JSONArrayJSONArray jArray = newJSONArray();
while (managedCursor.moveToNext())
{
    final Stringnumber = managedCursor.getString(number1);
    final String type2 = managedCursor.getString(type1);
    final String date = managedCursor.getString(managedCursor.getColumnIndexOrThrow("date")).toString();
    Date date1 = newDate(Long.valueOf(date));
    final String fDate = date1.toString();
    final String duration = managedCursor.getString(duration1);
    Stringtype = null;

    // Create JSONObjectJSONObject item = newJSONObject();

    // 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());

Solution 2:

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

JSONArray jArr = newJSONArray(strArr)

where strArr is your String array.

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

Post a Comment for "How To Convert Single String To Jsonarray In Android?"