Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Thursday, December 5, 2013 12:29 AM
function tableToJson(activeTab) {
var data = [];
// first row needs to be headers
var headers = [];
var header_length = $('#grd_'+(activeTab+1)+' tbody th').length;
for (var i = 0; i < header_length; i++) {
var header_name = $('#grd_' + (activeTab + 1) + ' tbody th')[i].innerHTML;
switch (header_name) {
case "Your Type":
headers[i] = "YourType";
break;
case "Code":
headers[i] = "YourCode";
break;
case "Your Type":
headers[i] = "YourType";
break;
case "Your life":
headers[i] = "YourLife";
break;
case "Period":
headers[i] = "YourPeriod";
break;
case "Qualifying Cost":
headers[i] = "OriginalCost";
break;
case "B/F Cost":
headers[i] = "Cost";
break;
case "Rate%":
headers[i] = "Rate%";
break;
case "Your Amount":
headers[i] = "YourRate";
break;
case "Your Amount":
headers[i] = "YourAmount";
break;
case "Your Carrying Amount":
headers[i] = "YourCarryingAmount";
break;
case "Last Date":
headers[i] = "YourGeneratedDate";
break;
default:
break;
}
}
// go through cells
var totalRowData_length = $('#grd_' + (activeTab + 1) + ' tbody tr').length;
for (var i = 1; i < totalRowData_length; i++) {
var tableRow = $('#grd_' + (activeTab + 1) + ' tbody tr')[i]
var tableRow_length = $('#grd_' + (activeTab + 1) + ' tbody tr')[i].cells.length;
var rowData = {};
for (var j = 0; j < tableRow_length; j++)
{
var dt = $('#grd_' + (activeTab + 1) + ' tbody tr')[i].cells[j].innerHTML;
if (dt.indexOf('INPUT') != -1) {
var mypattern = "value=\'?[0-9]*\.?[0-9]*\'?";
var myRegExp = new RegExp('(' + mypattern + ')', "g");
var result = dt.match(myRegExp);
var length = result.length;
if (result.length > 0)
dt = result[0].replace("value=", "");
else
dt = "0.00";
}
rowData[headers[j]] = dt;
}
data.push(rowData);
}
return data;
}
Is above valid Json ? How do I check whether it is valid?
After converted tables to Json array over clientside script, I got Invalid Json primitive error when passing the following code.
JavaScriptSerializer serializer = new JavaScriptSerializer();
List<ABC> lstABC = serializer.Deserialize<List<ABC>>("{"+((TextBox)Master.FindControl("txtABCString")).Text+"}");
What are the possible matter that can cause this problem. I had changed the header name to be exacted with ABC class. I am not sure what else could be the cause. There are date field in the class. Should I convert the date field to DateTime? But I do the conversion over client side..
After adding the "{" "}" I got the error below instead. What is it about really? what is the member name regarding to?
Invalid object passed in, member name expected. (1): {[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]}
All replies (2)
Monday, December 9, 2013 3:52 AM ✅Answered
Hi slen,
Thanks for posting to asp.net forum.
Based on your description and code, I see that you want to deserialize the json string to List<T>.
Please note that the format of json string should be look like below.
"[{Id:1,Name:'jhon',Birthday:'2013/2/3'},{Id:2,Name:'jhon2',Birthday:'2013/2/5'}]"
I made a demo for this issue.
public partial class DeSerialize : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
List<ABC> lstABC = serializer.Deserialize<List<ABC>>("[{Id:1,Name:'jhon',Birthday:'2013/2/3'},{Id:2,Name:'jhon2',Birthday:'2013/2/5'}]");
foreach (var abc in lstABC) {
Response.Write(abc.Id+" "+abc.Name+" "+abc.Birthday+"<br/>");
}
}
}
public class ABC {
public int Id { get; set; }
public string Name { get; set; }
public DateTime Birthday { get; set; }
}
So, we must make sure the json string in the Master.FindControl("...").Text is correct. Thanks.
Best Regards!
Monday, December 9, 2013 5:25 AM ✅Answered
Hi,
Better use "Json.js" for seralization of that.. It would be far more easier..
example :-
var arr = { City: 'Mumbai', Age: 25 };
$.ajax({
url: 'Ajax/MyData',
type: 'POST',
data: JSON.stringify(arr),
contentType: 'application/json; charset=utf-8',
dataType: 'json',
async: false,
success: function(msg) {
alert(msg);
}
});