www.icesr.com
IT运维工程师的摇篮

win10 uwp json

本文讲的是关于在uwp使用json的简单使用,json应用很多,因为我只是写简单使用,说的东西可能不对或者不符合每个人的预期。如果觉得我有讲的不对的,就多多包含,或者直接关掉这篇文章,但是请勿生气或者发怒吐槽,可以在我博客评论 http://blog.csdn.net/lindexi_gd

现在很多应用都是使用json

如果我们拿到一段json,想要把它变为我们C艹艹可以用的,我们需要先对json的类进行转换,其实很简单,我们在复制一段json

不需要我们对这json打,因为我们有vs,在我们的编辑,可以看到

这里写图片描述

我们复制完一段json,然后点击粘贴,就好了,自动生成

如果我们拿到一段json

<code>{
  "<span class="hljs-attribute">results</span>": <span class="hljs-value">[{
    "<span class="hljs-attribute">location</span>": <span class="hljs-value">{
      "<span class="hljs-attribute">id</span>": <span class="hljs-value"><span class="hljs-string">"WX4FBXXFKE4F"</span></span>,
      "<span class="hljs-attribute">name</span>": <span class="hljs-value"><span class="hljs-string">"北京"</span></span>,
      "<span class="hljs-attribute">country</span>": <span class="hljs-value"><span class="hljs-string">"CN"</span></span>,
      "<span class="hljs-attribute">path</span>": <span class="hljs-value"><span class="hljs-string">"北京,北京,中国"</span></span>,
      "<span class="hljs-attribute">timezone</span>": <span class="hljs-value"><span class="hljs-string">"Asia/Shanghai"</span></span>,
      "<span class="hljs-attribute">timezone_offset</span>": <span class="hljs-value"><span class="hljs-string">"+08:00"</span>
    </span>}</span>,
    "<span class="hljs-attribute">daily</span>": <span class="hljs-value">[{      
      "<span class="hljs-attribute">date</span>": <span class="hljs-value"><span class="hljs-string">"2015-09-20"</span></span>,           
      "<span class="hljs-attribute">text_day</span>": <span class="hljs-value"><span class="hljs-string">"多云"</span></span>,              
      "<span class="hljs-attribute">code_day</span>": <span class="hljs-value"><span class="hljs-string">"4"</span></span>,                 
      "<span class="hljs-attribute">text_night</span>": <span class="hljs-value"><span class="hljs-string">"晴"</span></span>,              
      "<span class="hljs-attribute">code_night</span>": <span class="hljs-value"><span class="hljs-string">"0"</span></span>,              
      "<span class="hljs-attribute">high</span>": <span class="hljs-value"><span class="hljs-string">"26"</span></span>,                
      "<span class="hljs-attribute">low</span>": <span class="hljs-value"><span class="hljs-string">"17"</span></span>,                 
      "<span class="hljs-attribute">precip</span>": <span class="hljs-value"><span class="hljs-string">"0"</span></span>,                 
      "<span class="hljs-attribute">wind_direction</span>": <span class="hljs-value"><span class="hljs-string">""</span></span>,           
      "<span class="hljs-attribute">wind_direction_degree</span>": <span class="hljs-value"><span class="hljs-string">"255"</span></span>,   
      "<span class="hljs-attribute">wind_speed</span>": <span class="hljs-value"><span class="hljs-string">"9.66"</span></span>,            
      "<span class="hljs-attribute">wind_scale</span>": <span class="hljs-value"><span class="hljs-string">""</span>                 
    </span>}, {
      "<span class="hljs-attribute">date</span>": <span class="hljs-value"><span class="hljs-string">"2015-09-21"</span></span>,
      "<span class="hljs-attribute">text_day</span>": <span class="hljs-value"><span class="hljs-string">"晴"</span></span>,
      "<span class="hljs-attribute">code_day</span>": <span class="hljs-value"><span class="hljs-string">"0"</span></span>,
      "<span class="hljs-attribute">text_night</span>": <span class="hljs-value"><span class="hljs-string">"晴"</span></span>,
      "<span class="hljs-attribute">code_night</span>": <span class="hljs-value"><span class="hljs-string">"0"</span></span>,
      "<span class="hljs-attribute">high</span>": <span class="hljs-value"><span class="hljs-string">"27"</span></span>,
      "<span class="hljs-attribute">low</span>": <span class="hljs-value"><span class="hljs-string">"17"</span></span>,
      "<span class="hljs-attribute">precip</span>": <span class="hljs-value"><span class="hljs-string">"0"</span></span>,
      "<span class="hljs-attribute">wind_direction</span>": <span class="hljs-value"><span class="hljs-string">""</span></span>,
      "<span class="hljs-attribute">wind_direction_degree</span>": <span class="hljs-value"><span class="hljs-string">"157"</span></span>,
      "<span class="hljs-attribute">wind_speed</span>": <span class="hljs-value"><span class="hljs-string">"17.7"</span></span>,
      "<span class="hljs-attribute">wind_scale</span>": <span class="hljs-value"><span class="hljs-string">"3"</span>
    </span>}, {
    }]</span>,
    "<span class="hljs-attribute">last_update</span>": <span class="hljs-value"><span class="hljs-string">"2015-09-20T18:00:00+08:00"</span> 
  </span>}]
</span>}</code>

我们弄个新的类,粘贴

<code>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> Thinw
    {

        <span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> Rootobject
        {
            <span class="hljs-keyword">public</span> Result[] results { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
        }

        <span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> Result
        {
            <span class="hljs-keyword">public</span> Location location { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
            <span class="hljs-keyword">public</span> Daily[] daily { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
            <span class="hljs-keyword">public</span> DateTime last_update { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
        }

        <span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> Location
        {
            <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> id { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
            <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> name { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
            <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> country { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
            <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> path { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
            <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> timezone { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
            <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> timezone_offset { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
        }

        <span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> Daily
        {
            <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> date { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
            <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> text_day { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
            <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> code_day { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
            <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> text_night { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
            <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> code_night { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
            <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> high { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
            <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> low { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
            <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> precip { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
            <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> wind_direction { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
            <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> wind_direction_degree { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
            <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> wind_speed { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
            <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> wind_scale { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
        }
    }</code>

很快我们就得到了我们需要序列的类

接着我们使用Nuget

这里写图片描述

当然我还加上九幽的插件,九幽有几个插件可以获得我们应用数据,我们启动我们关闭,还有广告很好用

我们使用Nuget主要下载Newtonsoft.Json

我们转序可以使用

<code>        <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">Unencoding</span>(<span class="hljs-keyword">string</span> str)
        {
            <span class="hljs-keyword">var</span> json = JsonSerializer.Create();
            Rootobject thinw = json.Deserialize&lt;Rootobject&gt;(<span class="hljs-keyword">new</span> JsonTextReader(<span class="hljs-keyword">new</span> StringReader(str)));
        }</code>

如果我们需要把我们的类转为json

<code>            var file = await ApplicationData<span class="hljs-preprocessor">.Current</span><span class="hljs-preprocessor">.LocalFolder</span><span class="hljs-preprocessor">.CreateFileAsync</span>(<span class="hljs-string">"data"</span>, CreationCollisionOption<span class="hljs-preprocessor">.ReplaceExisting</span>)<span class="hljs-comment">;</span>
            using (TextWriter stream = new StreamWriter(await file<span class="hljs-preprocessor">.OpenStreamForWriteAsync</span>()))
            {
                json<span class="hljs-preprocessor">.Serialize</span>(stream, thinw)<span class="hljs-comment">;</span>
            }</code>

这样把我们的类保存在文件

如果觉得我做的简单,想要使用微软的Windows.Data.Json ,其实使用Newtosoft的才好

如果使用Windows.Data.Json

<code>JsonArray root = JsonValue<span class="hljs-preprocessor">.Parse</span>(jsonString)<span class="hljs-preprocessor">.GetArray</span>()<span class="hljs-comment">;  </span>
for (uint i = <span class="hljs-number">0</span><span class="hljs-comment">; i &lt; root.Count; i++) {  </span>
    string name1 = root<span class="hljs-preprocessor">.GetObjectAt</span>(i)<span class="hljs-preprocessor">.GetNamedString</span>(<span class="hljs-string">"name"</span>)<span class="hljs-comment">;  </span>
    string description1 = root<span class="hljs-preprocessor">.GetObjectAt</span>(i)<span class="hljs-preprocessor">.GetNamedString</span>(<span class="hljs-string">"description"</span>)<span class="hljs-comment">;  </span>
    string link1 = root<span class="hljs-preprocessor">.GetObjectAt</span>(i)<span class="hljs-preprocessor">.GetNamedString</span>(<span class="hljs-string">"link"</span>)<span class="hljs-comment">;  </span>
    string cat1 = root<span class="hljs-preprocessor">.GetObjectAt</span>(i)<span class="hljs-preprocessor">.GetNamedString</span>(<span class="hljs-string">"cat"</span>)<span class="hljs-comment">;  </span>
    string image1 = root<span class="hljs-preprocessor">.GetObjectAt</span>(i)<span class="hljs-preprocessor">.GetNamedString</span>(<span class="hljs-string">"image"</span>)<span class="hljs-comment">;  </span>
    var chan = new RootObject {  
        name = name1, cat = cat1, description = description1, image = image1, link = link1  
    }<span class="hljs-comment">;  </span>
    obs_channels<span class="hljs-preprocessor">.Add</span>(chan)<span class="hljs-comment">;  </span>
})<span class="hljs-comment">; </span>
</code>

如果属性多,基本上很多人会容易就打错,当然,可以先实例一个RootObject,然后使用新关键字,name去得到实例属性名称

当然在我们使用Json会遇到一些属性我们不要的,那么如何json忽略属性,其实很简单,在Newtosoft可以在属性加[JsonIgnore],因为这些比较乱,所以也不打算在这里说。

首先是我们的类,

<code>  <span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> Thine
  {
      <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> Property{<span class="hljs-keyword">set</span>;<span class="hljs-keyword">get</span>;}
      <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> Ignore{<span class="hljs-keyword">set</span>;<span class="hljs-keyword">get</span>;}
  }</code>

我们要把Property包含,但是不包含Ignore,简单的

<code>  <span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> Thine
  {
      <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> Property{<span class="hljs-keyword">set</span>;<span class="hljs-keyword">get</span>;}
      [JsonIgnore]
      <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> Ignore{<span class="hljs-keyword">set</span>;<span class="hljs-keyword">get</span>;}
  }</code>

但是有时候我们要包含很少,基本都是不包含的,那么如何只用包含,其实我们可以在类加[JsonObject(MemberSerialization.OptIn)]看到了OptIn,其实OptOut就是不包含一些,OptIn就是包含一些

<code>
  [JsonObject(MemberSerialization.OptIn)]
  <span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> Thine
  {
      [JsonProperty]
      <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> Property{<span class="hljs-keyword">set</span>;<span class="hljs-keyword">get</span>;}
      <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> Ignore{<span class="hljs-keyword">set</span>;<span class="hljs-keyword">get</span>;}
  }</code>

知识共享许可协议
本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。欢迎转载、使用、重新发布,但务必保留文章署名林德熙(包含链接:http://blog.csdn.net/lindexi_gd ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请与我联系

未经允许不得转载:冰点网络 » win10 uwp json

分享到:更多 ()

评论 抢沙发

评论前必须登录!