C#在后面的代码中反序列化json数组。

时间:2014-04-09 05:45:20

标签: c# json

我有一个如下所示的json数组:

{"division_ids":"[\"2\",\"3\",\"4\"]","particular_settings":"{\"sel_taxonomy\":\"\",\"particular_amount\":\"\",\"exclude_group\":null,\"discount_group_id\":\"-Select Group-\",\"discount_amount\":\"\",\"from_date\":\"\",\"to_date\":\"\"}"}

如何将其反序列化为两个阵列,其中包含' division_ids'和其他特殊设置'

3 个答案:

答案 0 :(得分:0)

Json.net library允许您反序列化对象。

dynamic json = JObject.Parse("your json string");
dynamic divisionIds = json.division_ids;
dynamic particularSettings = json.particular_settings;

Console.WriteLine(divisionIds[0]); // Output : 2

答案 1 :(得分:0)

你可以试试这个:

var jObject = JsonConvert.DeserializeObject<JObject>(yourJson);
var divList = jObject["division_ids"].ToObject<List<string>>();
var particularSettingList = jObject["particular_settings"].ToObject<List<string>>();

答案 2 :(得分:-1)

您可以使用jQuery和$ .parseJSON。我在下面举了一个例子。请注意,我必须对json进行两项更改才能使其生效:"[\"2\",\"3\",\"4\"]"变为[\"2\",\"3\",\"4\"]并删除第二个数组周围的双引号,"particular_settings":"{\"变为"particular_settings":{\"(删除相应的结束报价也是如此。

<script>

    function parseJson() {

       var myjson = '{"division_ids":[\"2\",\"3\",\"4\"],"particular_settings":{\"sel_taxonomy\":\"test",\"particular_amount\":\"\",\"exclude_group\":null,\"discount_group_id\":\"-Select Group-\",\"discount_amount\":\"\",\"from_date\":\"\",\"to_date\":\"\"}}';

        var obj = $.parseJSON( myjson );
        alert(obj.division_ids);
        alert(obj.particular_settings.sel_taxonomy);            

    }

</script>