在反序列化的json对象中按键列出值

时间:2015-07-02 02:04:23

标签: c# json object

所以我定义了

public class Globals
{
    public List<object> var { get; set; }
}

我的json我发送就像这样

{"var":[{"test1":"1","test2","2","test3","3"}],"morejson":"blahblahblah}

所以我现在说我已经有了这个,我们假装AppendTextBox正在一个单独的线程中写一个文本框

void getmedata()
{
    var commands = JsonParser.Deserialize<Globals>(json);

    AppendTextBox(commands.description[0].ToString()); 
    AppendTextBox(commands.description.Count.ToString());
}

AppendTextBox(commands.description.Count.ToString());有效并显示计数

然而,我似乎无法通过密钥来打印值。目前设置方式为输出

System.Collections.Generic.Dictionary`2[System.String,System.Object]`

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

您可以使用List Dictionary代替object使用public class Globals { public List<Dictionary<string, string>> var { get; set; } } 来访问它:

{"var":[{"test1":"1","test2","2","test3","3"}, {"anykey1": "anyvalue1", "anykey2": "anyvalue2"}, {}, {"1": "2"}], "morejson": "blahblahblah"}

然后,你的JSON

List

将转换为Dictionary<string, string>长度为4.每个键代表foreach (var keyValuePair in commands[1]) { Console.WriteLine("{0} => {1}", keyValuePair.Key, keyValuePair.Value); }

例如,如果你这样做:

anykey1 => anyvalue1
anykey2 => anyvalue2

你会得到

           <tr>
            <th class="surname">SURNAME</th>
            <th class="firstName">FIRST NAME</th>
            <th class="loginEmail">LOGIN EMAIL</th>
            <th class="customer">CUSTOMER</th>
            <th class="capabilities">CAPABILITIES</th>
            <th class="currentStatus">STATUS</th>
            <th class="lastInviteSent">LAST EMAIL SENT</th>
            <th class="removeReq"></th>
        </tr>