【问题标题】:How to use JSON for inserting records into SQL database如何使用 JSON 将记录插入 SQL 数据库
【发布时间】:2011-08-29 13:14:23
【问题描述】:

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

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

这是我的Android code

谢谢

【问题讨论】:

    标签: android sql-server web-services json ksoap


    【解决方案1】:

    在我的情况下,这很好用,

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

    试试看。谢谢。

    【讨论】:

    • 您好,感谢您的发帖!!我只是想知道一些关于你的代码的东西。什么是 "key1" 和 "key2" 。远程数据库 url 是我在代码中使用的 Web 服务 url 吗?
    • key1 和 key2 是您的数据插入远程数据库的键。在服务器端,您可以通过这些键获取数据。并且您的数据库服务器 url 在这里传递 -> HttpPost httppost = new HttpPost("yor remote database url");
    • 实际上现在我正在使用本地主机 ..我在 SQL Server 2008 中创建了我的数据库。那么在这种情况下,我的 url 应该是什么?另外,您的代码是否将字符串数组转换为 JSON 数组?抱歉,我对 Android 尤其是 JSON 很陌生。请问你能帮我吗?
    • 这里在 jsonObject.put("key1", value1); jsonObject.put("key2", value2); value1 和 value2 是你的 string[] 的元素,比如 string[0] 和 string[1] 值,所以它是字符串格式,所以不需要转换只是作为字符串传递。对于您本地主机的 url,我认为它是 10.0.2.2 或者可能是 10.0.2.1 环回地址。
    • 并说如果我的字符串数组有多达 70 个值..den 我应该把它放在一个 for 循环中吗?
    【解决方案2】:

    好吧,我只是想向您展示如何将 String 数组写入 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();
            }
    

    【讨论】:

      【解决方案3】:
          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);
      
                      }
      

      【讨论】:

      • 它在动作脚本中,在 flash builder 中,我使用 (.Net) Web 服务将数据从 sqlite 插入到 sql server。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-03
      • 1970-01-01
      • 1970-01-01
      • 2013-11-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多