转换JSON字符串以保存到数据库中 [英] Convert JSON string to save into the database

查看:503
本文介绍了转换JSON字符串以保存到数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个返回200的ajax调用。我有一个web服务,我想保存我希望保存到某些列的字符串中的某些字段,将整个字符串保存到另一列。



有没有办法使字符串可用,这样我就可以得到每个项目或从字符串中创建一个对象。



当我将鼠标悬停在web方法中的数据(word)上时:(调试)

I have a ajax call that returned a 200. I have a webservice that I want to save certain fields from the string I want to save into certain columns and the whole string into another column.

Is there a way to make the string use-able so I can get at each item or make an object from the string.

When I hover over the data(word) in the web method:(debugging)

[WebMethod]
   public string InsertformData(string data)
   {



我收到JSON字符串:


I get the JSON string:

data = "[{\"name\":\"formType\",\"value\":\"solarPV\"},{\"name\":\"inp-membershipNumber\",\"value\":\"1\"},{\"name\":\"inp-company-name\",\"value\":\"acme\"},{\"name\":\"inp-representative\",\"value\":\"dw\"},{\"name\":\"inp-position\",\"value\":\"dd\"},{\"name\":...





我希望能够保存对此:或者可以使用的东西。



I want to be able to save to this: or something workable.

cmd.Connection = conn;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText =
                    @"INSERT INTO tblFormData(formType, formAssessmentID, formDate, formContent) VALUES(@formType, @formAssessmentID,@formDate, @formContent)";
                cmd.Parameters.AddWithValue("@formType", data[0]);
                cmd.Parameters.AddWithValue("@formAssessmentID", data[1]);
                cmd.Parameters.AddWithValue("@formDate", data[6]);
                cmd.Parameters.AddWithValue("@formContent", data);





如何将字符串转换为可用对象?



How would I convert the string to useable objects?

推荐答案

有几个JSON (de)你可以使用的序列化器。像 fastJSON [ ^ ]或 Json.NET [ ^ ]您可以使用将json字符串反序列化为已知或动态对象。
There are several JSON (de)serializers you can use. Like fastJSON[^] or Json.NET[^] you can use to deserialize your json string into known or dynamic object.


这篇关于转换JSON字符串以保存到数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆