• C#直接解析Json键值对


    string test_json = "{"name":"tom","nickname":"tony","sex":"male","age":20,"email":"123@123.com"}";  
        var o = JObject.Parse(yourJsonString);  
          
                    foreach (JToken child in o.Children())  
                    {  
                        var property1 = child as JProperty;  
                        MessageBox.Show(property1.Name + ":" + property1.Value);  
        }  
    
    
    多层Json
    
    
        var o = JObject.Parse(yourJsonString);  
          
        foreach (JToken child in o.Children())  
        {  
            //var property1 = child as JProperty;  
            //MessageBox.Show(property1.Name + ":" + property1.Value);  
            foreach (JToken grandChild in child)  
            {  
                foreach (JToken grandGrandChild in grandChild)  
                {  
                    var property = grandGrandChild as JProperty;  
                    if (property != null)  
                    {  
                        MessageBox.Show(property.Name + ":" + property.Value);  
                    }  
                }  
            }  
        }  

    用Newtonsoft.Json.dll

    string str = @"{
    ""data"":
    {
    ""mobile"":
    {
    ""platform"":""2.2"",
    ""model"":""generic|sdk"",
    ""versioncode"":""20111125"",
    ""versionname"":""1.2.0"",
    ""web_domain"":""http://www.baidu.com"",
    ""ip"":"""",
    ""dns1"":"""",
    ""dns2"":"""",
    ""channel_id"":""0""
    }
    }
    }";
                JavaScriptObject json = (JavaScriptObject)JavaScriptConvert.DeserializeObject(str);
                JavaScriptObject data = (JavaScriptObject)json["data"];
                JavaScriptObject mobile = (JavaScriptObject)data["mobile"];
                Console.WriteLine("{0} {1} {2}", mobile["platform"], mobile["model"], mobile["versioncode"]);
     
    当然,如果有承接类也可以直接反序列化成承接类。
  • 相关阅读:
    c#结构体、打他table、excel、csv互转
    WPF 自定义图表(柱状图,曲线图)
    NemaStudio船舶模拟软件下载及破解
    点双连通分量
    HDU4612 Warm up
    边双连通分量
    [Jsoi2010]连通数
    Intern Day73
    Intern Day72
    Intern Day70
  • 原文地址:https://www.cnblogs.com/cuihongyu3503319/p/9204455.html
Copyright © 2020-2023  润新知