Convert normal Java Array or ArrayList to Json Array in android

前端 未结 8 1328
栀梦
栀梦 2020-11-29 06:15

Is there any way to convert a normal Java array or ArrayList to a Json Array in Android to pass the JSON object to a webservice?

相关标签:
8条回答
  • 2020-11-29 07:05

    example key = "Name" value = "Xavier" and the value depends on number of array you pass in

     try
          {
          JSONArray jArry=new JSONArray();
          for (int i=0;i<3;i++)
          {
           JSONObject jObjd=new JSONObject();
           jObjd.put("key", value);
           jObjd.put("key", value);
           jArry.put(jObjd);
          }
          Log.e("Test", jArry.toString());
          }
     catch(JSONException ex)
         {
    
         }
    
    0 讨论(0)
  • 2020-11-29 07:05

    My code to convert array to Json

    Code

    List<String>a = new ArrayList<String>();
    a.add("so 1");
    a.add("so 2");
    a.add("so 3");
    JSONArray jray = new JSONArray(a);
    
    System.out.println(jray.toString()); 
    

    output

    ["so 1","so 2","so 3"]
    
    0 讨论(0)
提交回复
热议问题