2011-08-29 98 views
2

假设我在字符串数组中存储了一个名称列表,例如:“abc”,“bcd”,“gdf”...。我有一个Android应用程序,显示每个值以及一个复选框。我需要将我的字符串数组转换为JSON字符串,以便我可以将其存储在远程数据库中。现在我正在使用使用SQL Server创建的数据库来处理本地主机。我需要使用Web服务在数据库中插入JSON字符串值,最好是SOAP如何使用JSON将记录插入到SQL数据库中

我该怎么做?还有其他更好的方法吗?

这是我的Android code

感谢

回答

2

在我的情况下能正常工作,

  JSONObject jsonObject = new JSONObject(); 

      jsonObject.put("key1", value1); 
      jsonObject.put("key2", value2); 

      JSONArray jArrayParam = new JSONArray(); 
      jArrayParam.put(jsonObject); 

      List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(); 
      nameValuePair.add(new BasicNameValuePair("bulkdata", 
        jArrayParam.toString())); 

      Log.e("bulkdata", jArrayParam.toString()); 

     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost("yor remote database url"); 

     httppost.addHeader("Content-Type", "application/x-www-form-urlencoded"); 
     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8)); 
     // Execute HTTP Post Request 
     HttpResponse response = httpclient.execute(httppost); 
     // get response entity 
     HttpEntity entity = response.getEntity(); 

试试吧。日Thnx。

+0

嘿谢谢张贴!我只想知道一些关于你的代码的东西。什么是“key1”和“key2”。远程数据库网址是我在代码中使用的网络服务网址吗? –

+0

key1和key2是通过其将数据插入远程数据库的密钥。在服务器端,您可以通过这些键获取数据。你的数据库服务器的URL是通过这里 - > HttpPost httppost =新的HttpPost(“yor远程数据库url”); – user370305

+0

其实现在我在本地主机工作..我有我的数据库在SQL Server 2008中创建。所以在这种情况下应该是我的网址?你的代码是否将字符串数组转换为JSON数组?对不起,我对Android很陌生,特别是JSON。请你能帮助我吗? –

1

嗯,我只是试图告诉你如何将字符串数组写入JSONObject和JSONArray。希望这也能帮助你。

String arr[] = {"1","parth","present","30-82011","Mumbai"}; 

try { 
       JSONObject obj=new JSONObject(); 
       obj.put("rollno",new Integer(arr[0])); 
       obj.put("status",arr[1]); 
       obj.put("date",arr[2]); 
       obj.put("place",arr[3]); 
       System.out.print(obj.toString(1)); 

       JSONArray list = new JSONArray(); 
       list.put(arr[0]); 
       list.put(arr[1]);    
       list.put(arr[2]);    
       list.put(arr[3]);    
       System.out.print(list.toString(1)); 
       System.out.println(""); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
0
var arr:String = com.adobe.serialization.json.JSON.encode(Obj); 
var data_projects:Array = stmt.getResult().data; 
var b_data:String = com.adobe.serialization.json.JSON.encode(data_projects); 
var arr:String = com.adobe.serialization.json.JSON.encode(data_projects); 
var arr1:Object = com.adobe.serialization.json.JSON.decode(b_data) as Array; 

       for(var d:int=0;d<=data_projects.length-1;d++) 
         
       { 
//Mapping properties of Proxy classes with actual fields 
       var bbb:Object = new Object; 
       data.MdId = arr1[d].MD_ID; 
       data.MdDevId=arr1[d].MD_DEVICE_ID; 
       data.MdRecId=arr1[d].MD_REC_ID; 
       data.MdPrjId= arr1[d].MD_PRJ_ID ; 
       data.MdMbcId = arr1[d].MD_MBC_ID; 
       data.MdMbcValue= arr1[d].MD_MBC_VALUE; 
       data.MdParentRecId= arr1[d].MD_MBC_ID; 
//below is the create method on the WSDL 
       ws.Create(data); 

       } 
+0

它是在动作脚本中,在Flash Builder中,我使用(.Net)Web服务从sqlite将数据插入到sql server。 – johnny

相关问题