Serializing Two Dimensional Array With Newtonsoft.json
Apr 20, 2010
I am getting an error when trying to serialize an object products.
Product product = new Product();
product.Name = "Apple";
product.Expiry = new DateTime(2008, 12, 28);
product.Price = 3.99M;
product.Sizes = new string[3,2] { {"Small","40"}, {"Medium","44"}, {"Large","50"} };
string json = JsonConvert.SerializeObject(product);//this line is throwing an error
Array was not a one-dimensional array. Is there any way to serialize a two dimensional array with Newtonsoft.json.
View 2 Replies
Similar Messages:
Feb 19, 2010
First I am sorry if I am creating a thread in the wrong category as I am not sure which is the best suitable place for this question: I want to create a user defined data structure, which will be a 2 dimensional array such that each individual square of this 2-dimensional array will also store the score and the grades. ( Iam using VB.NET) More details: for example: There is an 2 dimensional array with 3 columns and 2 rows, I want to fill each of the (0,0);(0,1);(0,2) with their own score (Integer type) and grades (String Type). And this is same for the rest of the squares i.e. (1,0);(1,1);(1,2) and (2,0);(2,1);(2,2) After creating such custom data structure: "CustomArray", I should be able to create the instance of this custom data structure (which will be a class) and code something like this:
Dim objCustomArray as CustomArray
objCustomArray = New CustomArray
for i as Integer =0 to 3
objCustomArray(0,i).SetScore(1)
objCustomArray(0,i).SetGrade("Good")
Next
-------
for i as Integer =0 to 3
Dim myVal as Integer = objCustomArray(0,i).GetScore()
Dim myGrade as String = objCustomArray(0,i).gettGrade()
Next
-----
View 4 Replies
Jun 22, 2010
I have a 2 dimensional array, I want to convert it to an Excel document using C#.
View 2 Replies
Jul 23, 2010
Can we use a 2 dimensional array to bind it in our form? In my model I have this:
public class EmailDetails {
public string[] emailTo { get; set; }
public string[,] emailFieldValues { get; set; } }
So in my form to bind the emailTo is simple and this one works:
<input type="text" id="Text1" title=" " name="emailTo" maxlength="100" />
<input type="text" id="Text1" title=" " name="emailTo" maxlength="100" />
I need a 2 dimensional array so that I can match the value with something,
like this:<input type="text" id="Text1" title=" " name="EmailDetails["@name",valueofinput] maxlength="100" />
View 4 Replies
Feb 14, 2011
I have an object called MyObject that has several properties. MyList is a list of MyObject that I populate with a linq query and then I serialize MyList into json. I end up with something like this
List<MyObject> MyList = new List<MyObject>();
MyList = TheLinqQuery(TheParam);
var TheJson = new System.Web.Script.Serialization.JavaScriptSerializer();
string MyJson = TheJson.Serialize(MyList);
What I want to do is serialize only parts of MyObject. For instance, I might have Property1, Property2...Propertyn and I want MyJson to only include Property3, Property5 and Property8.
I thought of a way to do this by creating a new object with only the properties I want and from there create a new list for the serialization.
View 1 Replies
Apr 6, 2010
I have this array: string[,] productData = new string[5,7];
I bind it to a repeater and a call a method like: <img src="<%# getPhoto1WithReplace(Container.ItemIndex) %>"
Which is defined like:
public String getPhoto1WithReplace(Object itemIndex) {
int intItemIndex = Int32.Parse(itemIndex.ToString());
if (productData[intItemIndex, 3] != null) return this.ResolveUrl(productData[intItemIndex, 3].ToString());
else return String.Empty; }
I do not understand why it calls getPhoto1WithReplace with itemIndex as 5. My array has 5 indexes: 0,1,2,3,4, so How Container.ItemIndex can be 5?
View 2 Replies
Oct 15, 2010
I've created an Excel spreadsheet for my users to upload into my web application to bulk import memberships and roles into the application. There are up to 4 different roles which users can be members of so I'm using AddUsersToRoles to add the user to one or more roles in one line of code like this:
r.AddUsersToRoles(usernames, CreateListOfRoles(Role1, Role2, Role3, Role4))
As this line of code expects a one-dimensional array of roles, I have created a function to take in the roles from my spreadsheet, add them into a one-dimensional array, and return that to the AddUsersToRoles method. The CreateListOfRoles function looks like this:
[Code]....
The error that I'm getting though is "System.InvalidCastException: At least one element in the source array could not be cast down to the destination array type." I've stepped through the Function in debug mode and all the values look correct and there are the correct number of items in the array so I don't understand what's going wrong.
View 3 Replies
Sep 28, 2010
I have the following code:
<script type="text/javascript">
var checksSinceLastPostBack = new Array();
function clientSelectedIndexChanged(sender, eventArgs) {
var ajaxManager = $find("<%= RadAjaxManager.GetCurrent(Page).ClientID %>");
var serializedCheckData = checksSinceLastPostBack.serializeArray();
if (ajaxManager != null)
ajaxManager.ajaxRequest(serializedCheckData);
}
</script>
The
var serializedCheckData = checksSinceLastPostBack.serializeArray();
doesn't seem to work. Am I misunderstanding this?
Also if this works, how would I deserialize it in the code behind?
View 1 Replies
Jul 6, 2010
I have this data:
5-16-2010, 5
5-16-2010, 3
5-16-2010, 2
5-16-2010, 4
5-15-2010, 2
5-15-2010, 5
5-15-2010, 1
5-14-2010, 3
5-14-2010, 5
I want to group by the date, these values are now in a dataset. What comes up to my mind is an array list with the following: The master array would have each date listed on the dataset above, within each row of this array I would have the list of numbers for that specific date. How do I do that in VB.NET?
View 2 Replies
Oct 8, 2010
How do you store multi-dimensional array to viewstate? Here's what I did:
public bool[,,] regsettings = new bool[7,5,4];
bool[,,] regsettings = (bool[,,])ViewState["regsettings"]; // this line works!
regsettings[i,j,tab] = (bool)ViewState["regsettings"]; // this line no working?! The error is: Object reference not set to an instance of an object. The letters i,j, tab are just variables in the loop. They are there. I also tried: (bool[,,,]) as a cast and it is a no-go either.?
View 1 Replies
Nov 13, 2010
I know I can use arrayName.length to find out how many items in total in the array there are, but how can I gracefully find out how many items are in a specific dimension? I know I could probably do it in a for each loop, but it would look a little messy, does anyone know of a simple way that I may have overlooked?
View 3 Replies
Mar 11, 2010
My original code was in classic vb. and could use redim array to resize them. I was porting the code to c# now and lately i have run into an issue to resize a 2 dimensional array. The thing here is I have a 2 dimensional array whose 2nd dimension needs to be increased dynamically as needed to accommodate the data. For say
String[,] ary1=new String[2,10];
I'm doing some operation in a loop and filling in the data in ary1. And if the loop still continues, on reaching the ary1 upper bound i need to increase the size further to accommodate 10 more rows. I tried couple of ways to copy it to a bigger array and assign it back to the current array. but somehow dint work.
View 1 Replies
Feb 16, 2011
I have a web form where I create an array of type structure.
In this form I have:
[code]....
but this is not OK.
View 1 Replies
Apr 21, 2010
.I am using VS2010 Ultimate in an out of the box HelloWorld MVC2 ProjectI am using JQuery 1.4.1I have the following code in my View:
function TestStringArray() {
var stringArray = new Array();
stringArray[0] = "item1";
[code]...
Forgetting my controller actually needs to return a Json result (I have a break on the return View() line), the values parameter is null. When I pass in a single string (change the type from List<String> to String in the controller, pass in a single string value from the View), it works fine. Looking at Fiddler, the array is passing the multiple values.I did some research and people are suggesting JSON.Stringify ion the JSON2 library, the JSON.NEt library, etc... Do I really need extra libraries? Microsoft didn't think of this scenario for out of the box? I assume the problem is any kind of complex type.
View 2 Replies
Feb 28, 2011
I've been trying to deserialize the following JSON output into a vb.net object:
[Code]....
The following is what i've done so far:
[Code]....
I have problems reading the array value of "t". I'm quite sure that the problem lies in the PageTags() but i'm not sure how to go from here.
View 3 Replies
Mar 8, 2011
I'm using jqPlot and I need to turn this JSON which I receive from a WCF service:
[{ "x": 2, "y": 3 }, { "x": 25, "y": 34 }]
into this array or arrays:
[[2,3],[25,34]]
View 2 Replies
Jun 3, 2010
Wondering how to deserialize the following string in c#:
"[{"access_token":"thisistheaccesstoken"}]"
I know how to do it if the json was:
"{array=[{"access_token":"thisistheaccesstoken"}]}"
I'd do it like this:
public class AccessToken
{
public string access_token {get;set;}
public DateTime expires { get; set; }
}
public class TokenReturn
{
public List<AccessToken> tokens { get; set; }
}
JavaScriptSerializer ser = new JavaScriptSerializer();
TokenReturn result = ser.Deserialize<TokenReturn>(responseFromServer);
View 1 Replies
May 17, 2010
I'm looking for a way to define and send a JSON object array. I've figured out how to define a single JSON object, turn it into a string and send it, but what about an array of this type? Probably something simple I'm overlooking...
var myColumnSetting = { "ColumnName": name,
"ColumnIndex": index
}
convert it to a string
var myJSONText = JSON.stringify(myColumnSetting, false);
View 1 Replies
Mar 14, 2011
I am trying to return some data as json from an action method.
I have an employee object that looks like this:
public class Employee
{
public int EmployeeID {get; set;}
public string FirstName {get; set;}
public string LastName {get; set;}
//Other irrelevant properties
}
Then I have a view model as follows
public Class EmployeeViewModel
{
public Employee Supervisor{get; set;}
public List<EmployeeViewModel> Employees
}
I need to return a json object that looks exactly like this:
{id: 1, name: John, children: [
{id: 1, name: Joe, children: []},
{id: 1, name: Rob, children: []}
]}
For now I only need to go to the second level as above, returning and supervisor and their staff members beneath them.
How would I go about returning this in my action method(I have the viewModel object hydrated already I just need to return it as json). My problem so far has been that the children property does not get populated.
View 1 Replies
Apr 27, 2016
my JSON is as follows :
{ "Data":[ { "id":"1", "Name":"Sachin" }, { "id":"2", "Name":"Rahul" },
{ "id":"3", "Name":"Sovrav" } ]}
Now i want to filter out only array from that JSON and store them in a variable like this :
[ { "id":"1", "Name":"Sachin" }, { "id":"2", "Name":"Rahul" },
{ "id":"3", "Name":"Sovrav" } ]
View 1 Replies
Jul 21, 2010
For some reason the following JSON string creates the correct number of records in the custom objects array, but does NOT populate the objects in the array with and values.
[code]....
View 2 Replies
Feb 7, 2011
I have added a webservice to my website that references NewtonSoft.JSON library and it is giving me the following error
'Formatting' is an ambiguous reference between 'Newtonsoft.Json.Formatting' and 'System.Xml.Formatting'
I obviously don't want to change the .net page below, has anybody come across this issue before? Is there any way to disable the refernce for web services?
View 2 Replies
Jun 14, 2010
I have downloaded Json.net dll from http://json.codeplex.com/releases/view/43775. I extracted asp.net 2.0 dll and copied into my application bin folder under applicationin folder in inetpubwwwroot.Then I added json refrence in inline aspx code page as<%@ Import Namespace="Newtonsoft.Json" %>Now whenever I try to run my aspx page, it gives me following errorThe type or namespace name 'Newtonsoft' could not be found (are you missing a using directive or an assembly reference?) at System.Web.Compilation.AssemblyBuilder.Compile()
View 5 Replies
Aug 11, 2010
I try to use the jquery + json to get all elements in form and build a JSON var to post in a ASP.NET MVC method.
[Code]....
It method get all fields in a form and build a JSON, but it dont put JSON inside JSON.
Example:
If i have the follow form:
<input name="person.name"><input name="person.age"><input name="person.address.street">
The serialized string build a JSON like this
{ "person.name": "??", "person.age": "??", "person.address.street": "??" }
I need a plugin or some function to generate like this:
{ "person": { "name" : "??", "age" : "??", "address":{ "street": "??" } } }
View 1 Replies
Jun 16, 2010
I am looping through a dataset and I would like to have the following in this array list
name, from, to, price
john, 40, 50, 39.99
mike, 20, 50, 49.99
etc.
Once I do that, or if it is possible to do it using the dataset that will work with me. But I want to do...
If john's price between from and to...
So in my code I wanted to make it easy for me and instead of memorizing the row #, I would just do.. arraylist.row("john").item("from")
View 6 Replies