分配JSON对象属性模型 [英] Assigning Json object properties to a model

查看:82
本文介绍了分配JSON对象属性模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从匹配的嵌套JSON对象约90属性,使用动态和模型类JObject.Parse:

I'm matching about 90 properties from a nested JSON-Object to a model class using dynamics and JObject.Parse:

dynamic json = JObject.Parse(JsonObjectAsString);
return new Foo() {
    prop1 = json.summer.temperature,
    prop2 = json.winter.temperature,
    ...
    prop100 = json.autumn.temperature
}

这是非常缓慢的,但仅在第一次运行。在第二次运行,新的JSON对象被取出(相同的结构,但不同的值),而这一次被分配给在任何时间相应的模型属性。

This is very slow, but only on the first run. On a second run, a new JSON-Object is fetched (same structure but different values), and this one is assigned to the corresponding model properties in no time.

< STRONG>这是为什么? ?我能做些什么,以加快第一次运行

编辑:我已经设置生成类型为释放

I've already set the build type to "Release".

推荐答案

您可以分析您的JSON对象直接使用 newtonsoft 的NuGet包进行建模。

You can parse your JSON object direct to model using newtonsoft nuget package.

var objData = JsonConvert.DeserializeObject<MyData>(yourjsondata);

您可以从的 http://json2csharp.com/

您可以使用动态对象以及转换

You can convert using dynamic object as well

var objData = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(jsonString); 

或者不指定型号CALSS

Or without specifying model calss

var objData = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonString);

这篇关于分配JSON对象属性模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆