Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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)
C#
[WebMethod]
   public string InsertformData(string data)
   {

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.
C#
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?
Posted
Updated 4-Feb-15 5:39am
v2

1 solution

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.
 
Share this answer
 
Comments
StudentRik 5-Feb-15 4:42am    
Thank you. I have used JsonConvert.DeserializeObject(data), Do I need to make a class file to set my name: value pairs? A for loop? I don't know what to do next. any help would be great thank you.
Zoltán Zörgő 5-Feb-15 4:49am    
Try the ones I have recommended. Yes, you should make a class for it. With these you can deserialize to List<yourclass>.
StudentRik 5-Feb-15 5:05am    
Thank you, do you know of any examples or posts showing this?
Zoltán Zörgő 5-Feb-15 8:30am    
Something like this should work with fastjson:
List<yourclass> wallets = JSON.ToObject<List<yourclass>>(jsonstring);</yourclass></yourclass>
For Json.NET check this: http://www.newtonsoft.com/json/help/html/DeserializeObject.htm

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900