c# - 如果解析不好,则解析 JSON 语法

标签 c# json json.net win-universal-app

如果不能很好地从 newtonsoft json 解析 JSON 数据,我该如何解析它。请引用我下面的代码:

var web_uri = new Uri("www.example.com"); 
var resp = await client2.GetAsync(web_uri); 
var resp_to_str = await resp.Content.ReadAsStringAsync(); 
var json_obj = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(resp_to_str);

最后我解析了 JSON。现在,它按预期生成。

   {
        "Sex": "Male",
        "category": "A",
        "ID": 14,
        "created": "2016-03-03",
        "Tag": "2340",
        "members": [{
            "type": "A",
            "name": "fam_mem",
            "state": "ca",
            "Family": {
                "myGuardName": "tony",
                "details": [{
                    "address": "ca",
                    "type": "A"
                }]
            }
        }]
    }



  **RootObject omyclass = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(json_obj);** 

现在我在上面的行中遇到错误: System.Linq.Expressions.dll 中发生“Microsoft.CSharp.RuntimeBinder.RuntimeBinderException”类型的异常,但未在用户代码中处理

其他信息:“Newtonsoft.Json.JsonConvert.DeserializeObject(string)”的最佳重载方法匹配有一些无效参数

public class Detail
{
    public string address { get; set; }
    public string type { get; set; }
}

public class Family
{
    public string myGuardName { get; set; }
    public List<Detail> details { get; set; }
}

public class Member
{
    public string type { get; set; }
    public string name { get; set; }
    public string state { get; set; }
    public Family Family { get; set; }
}

public class RootObject
{
    public string Sex { get; set; }
    public string category { get; set; }
    public int ID { get; set; }
    public string created { get; set; }
    public string Tag { get; set; }
    public List<Member> members { get; set; }
}

TextBlock.Text = omyclass

我已更新问题

最佳答案

希望有帮助:

var web_uri = new Uri("www.example.com");
var resp = await client2.GetAsync(web_uri);
var resp_to_str = await resp.Content.ReadAsStringAsync();
RootObject omyclass = JsonConvert.DeserializeObject<RootObject>(resp_to_str); //pass the response string here.

根据OP的评论更新:

textBlock2.Text = omyclass + "----!"; 不起作用,因为 omyclass 是 RootObject,而不是 string

您必须获取所需的信息并将其附加到 textBlock2:

textBlock2.text = omyclass.Sex + "----!";


更新2(OP获取字符串作为键值对):

用法:textBlock2.text = omyclass + "----!";

重写RootObject.ToString()并使用Reflection获取属性和属性值

public class RootObject
{
    public string Sex { get; set; }
    public string category { get; set; }
    public int ID { get; set; }
    public string created { get; set; }
    public string Tag { get; set; }
    public List<Member> members { get; set; }

    public override string ToString()
    {
        var values = new List<string>();
        foreach (var property in GetType().GetProperties())
        {
            values.Add(property.Name + ": " + property.GetValue(this));
        }
        return string.Join(", ", values);
    }
}

关于c# - 如果解析不好,则解析 JSON 语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35762633/

相关文章:

c# - 为什么我不能在 Nancy 中注册自定义 JSON.Net 实现?

c# - JSON.net 将 C# 对象序列化为 JSON 问题

c# - 使用嵌套数组进行 LINQ to JSON 查询的正确语法

c# - Asp.net核心继续使用过期的证书

c# - Web API 2 - 实现补丁

javascript - typescript 并将对象转换为字符串

javascript - 类型错误 : Cannot read property 'first_name' of undefined on es6 model

c# - 从任务体内取消任务

c# - 使用 Roslyn 和 .NET Core 生成 C# 代码

c# - 由于依赖程序集的版本号,依赖程序集未复制到输出目录