[CLOSED] Cannot deserialize JSON object into type 'System.Collections.Generic.Dictionary`2[System.String,System.String][]

  1. #1

    [CLOSED] Cannot deserialize JSON object into type 'System.Collections.Generic.Dictionary`2[System.String,System.String][]

    Hi...

    i have kept this code in Page_Load,when try to run this im getting Error:"Cannot deserialize JSON object into type 'System.Collections.Generic.Dictionary`2[System.String,System.String][]"





    string r ="{ID:1,Name:Hi Rest}";
                    Dictionary<string, string>[] companies = JSON.Deserialize<Dictionary<string, string>[]>(r);
                   
    
                    StringBuilder sb = new StringBuilder();
    
                    foreach (Dictionary<string, string> row in companies)
                    {
                        foreach (KeyValuePair<string, string> keyValuePair in row)
                        {
                            sb.Append(keyValuePair.Key);
                            if (keyValuePair.Key.ToString() == "ID")
                            {
                                txtID.Text = Convert.ToString(keyValuePair.Value);
                            }
                            else if (keyValuePair.Key.ToString() == "Name")
                            {
                                txtName.Text = Convert.ToString(keyValuePair.Value);
                            }
                        }
                    }
    Please solve my problem
    Last edited by Daniil; Feb 02, 2011 at 10:43 AM. Reason: [CLOSED]
  2. #2
    Hi,

    Let's consider this string
    string r = "{ID:1,Name:Hi Rest}";
    If you need array please use square brackets in json string - '[ array items ]'.

    Also wrap string in apostrophes if there is any space characters - 'Hi Rest'.

    Generally speaking, it would be best to use apostrophes even there is no space character - '1'.

    So, in according to the Type that you chose
    Dictionary<string, string>[]
    string must look like this
    string r = "[{ID:'1',Name:'Hi Rest'}]";
  3. #3
    Quote Originally Posted by Daniil View Post
    Hi,

    Let's consider this string
    string r = "{ID:1,Name:Hi Rest}";
    If you need array please use square brackets in json string - '[ array items ]'.

    Also wrap string in apostrophes if there is any space characters - 'Hi Rest'.

    Generally speaking, it would be best to use apostrophes even there is no space character - '1'.

    So, in according to the Type that you chose
    Dictionary<string, string>[]
    string must look like this
    string r = "[{ID:'1',Name:'Hi Rest'}]";

    Hi, it is workin fine ..but i have One Dought..

    Without using these "[]" we cannot deserilize

    Any Alternative way is there.
  4. #4
    If you need
    Dictionary<string, string>[]
    square brackets are required.

    Well, without square brackets it will just, for example:
    Dictionary<string, string>
    Example
    <%@ Page Language="C#" %>
    
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    
    <script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
            string r = "{ID:'1', Name:'Hi Rest'}";
            Dictionary<string, string> company = JSON.Deserialize<Dictionary<string, string>>(r);
    
    
            foreach (KeyValuePair<string, string> keyValuePair in company)
            {
                if (keyValuePair.Key.ToString() == "ID")
                {
                    TextField1.Text = Convert.ToString(keyValuePair.Value);
                }
                else if (keyValuePair.Key.ToString() == "Name")
                {
                    TextField1.Text = Convert.ToString(keyValuePair.Value);
                }
            }
        }
    </script>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Ext.Net Example</title>
    </head>
    <body>
        <form runat="server">
        <ext:ResourceManager runat="server" />
        <ext:TextField ID="TextField1" runat="server" />
        <ext:TextField ID="TextField2" runat="server" />
        </form>
    </body>
    </html>
  5. #5
    Quote Originally Posted by Daniil View Post
    If you need
    Dictionary<string, string>[]
    square brackets are required.

    Well, without square brackets it will just, for example:
    Dictionary<string, string>
    Example
    <%@ Page Language="C#" %>
    
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    
    <script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
            string r = "{ID:'1', Name:'Hi Rest'}";
            Dictionary<string, string> company = JSON.Deserialize<Dictionary<string, string>>(r);
    
    
            foreach (KeyValuePair<string, string> keyValuePair in company)
            {
                if (keyValuePair.Key.ToString() == "ID")
                {
                    TextField1.Text = Convert.ToString(keyValuePair.Value);
                }
                else if (keyValuePair.Key.ToString() == "Name")
                {
                    TextField1.Text = Convert.ToString(keyValuePair.Value);
                }
            }
        }
    </script>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Ext.Net Example</title>
    </head>
    <body>
        <form runat="server">
        <ext:ResourceManager runat="server" />
        <ext:TextField ID="TextField1" runat="server" />
        <ext:TextField ID="TextField2" runat="server" />
        </form>
    </body>
    </html>

    Hi,

    Thank's for your reply. It is working fine

Similar Threads

  1. Replies: 8
    Last Post: Jul 20, 2012, 1:33 PM
  2. [CLOSED] Unsupported type: System.Guid (Migration from 0.8.3 to 1.0)
    By vedagopal2004 in forum 1.x Legacy Premium Help
    Replies: 2
    Last Post: Feb 07, 2011, 8:16 AM
  3. Replies: 0
    Last Post: Dec 27, 2009, 1:47 PM
  4. Replies: 2
    Last Post: Aug 31, 2009, 6:03 PM
  5. Replies: 3
    Last Post: Nov 12, 2008, 5:16 AM

Posting Permissions