如何使用 JSON 将记录插入 SQL 数据库

发布于 2024-12-02 02:05:30 字数 323 浏览 2 评论 0原文

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

我应该如何执行此操作?还有其他更好的方法吗?

这是我的 Android 代码

谢谢

Let's say I store a list of names , for eg: "abc","bcd","gdf"... in an array of Strings. I have an Android app that displays each of those values along with a checkbox. I need to convert my String array into a JSON String so that I can store it in a remote database. Right now I am working on localhost with a database created using SQL Server. I need to insert the JSON string values in the database using a web service , preferably SOAP

How should I do this ? Is there any other better way to do so ?

Here is my Android code.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

只是偏爱你 2024-12-09 02:05:30

就我而言,这效果很好,

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

尝试一下。谢谢。

In my case this works fine,

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

Try it. Thnx.

寄居人 2024-12-09 02:05:30

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

Well, I just tried to show you how to write the String array to JSONObject and 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();
        }
回首观望 2024-12-09 02:05:30
    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);

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

                }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文