简体   繁体   English

如何使用 Newtonsoft 将 json 字符串转换为 json 数组?

[英]How can I convert a json string to a json array using Newtonsoft?

I am using this code to read a json file firstSession.json and display it on a label.我正在使用此代码读取 json 文件firstSession.json并将其显示在 label 上。

var assembly = typeof(ScenarioPage).GetTypeInfo().Assembly;
string jsonFileName = "firstSession.json";
Stream stream = assembly.GetManifestResourceStream($"{assembly.GetName().Name}.{jsonFileName}");
using (var reader = new StreamReader(stream)) 
{
    var json = reader.ReadToEnd(); //json string
    var data = JsonConvert.DeserializeObject<SessionModel>(json);
    foreach (SessionModel scenario in data)
    {
        scenarioName.Text = scenario.title; 
        break;
    }
    scenarioName.Text = data.title; // scenarioName is the name of the label

}

SessionModel.cs looks like: SessionModel.cs看起来像:

    public class SessionModel : IEnumerable
    {
        public int block { get; set; }
        public string name { get; set; }
        public string title { get; set; }
        public int numberMissing { get; set; }
        public string word1 { get; set; }
        public string word2 { get; set; }
        public string statement1 { get; set; }
        public string statement2 { get; set; }
        public string question { get; set; }
        public string positive { get; set; } // positive answer (yes or no)
        public string negative { get; set; } // negative answer (yes or no) 
        public string answer { get; set; } // positive or negative 
        public string type { get; set; }
        public string format { get; set; }
        public string immersion { get; set; }

        public IEnumerator GetEnumerator()
        {
            throw new NotImplementedException();
        }

    }

The beginning of my json is:我的 json 的开头是:

{
  "firstSession": [
    {
      "block": 1,
      "name": "mark",
      "title": "mark's house",
      "numberMissing": 1,
      "word1": "distracted",
      "word2": "None",
      "statement1": "string 1",
      "statement2": "None",
      "question": "question",
      "positive": "No",
      "negative": "Yes",
      "answer": "Positive",
      "type": "Social",
      "format": "Visual",
      "immersion": "picture"
    },

I am getting a Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON object into type "MyProject.SessionModel" because the type requires a JSON array to deserialize correctly. To fix this error either change the JSON to a JSON array or change the deserialized type so that it is a normal .NET type that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path 'firstSession', line 2, position 17. I am getting a Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON object into type "MyProject.SessionModel" because the type requires a JSON array to deserialize correctly. To fix this error either change the JSON to a JSON array or change the deserialized type so that it is a normal .NET type that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path 'firstSession', line 2, position 17. Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON object into type "MyProject.SessionModel" because the type requires a JSON array to deserialize correctly. To fix this error either change the JSON to a JSON array or change the deserialized type so that it is a normal .NET type that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path 'firstSession', line 2, position 17.

How can I convert the json string to a json array?如何将 json 字符串转换为 json 数组? Or make one of the other modifications the debugger suggests?或者进行调试器建议的其他修改之一?

you need to create a wrapper class (json2csharp.com will help you do this)你需要创建一个包装器 class (json2csharp.com 将帮助你做到这一点)

public class Root    {
    public List<SessionModel> firstSession { get; set; } 
}

then然后

var data = JsonConvert.DeserializeObject<Root>(json);

data.firstSession will be a List<SessionModel> data.firstSession将是一个List<SessionModel>

  1. Create a new Class and have firstSession as List of SessionModel.创建一个新的 Class 并将 firstSession 作为 SessionModel 列表。

     public class Sessions { public List<SessionModel> firstSession { get; set; } }
  2. Remove IEnumerable from the SessionModel从 SessionModel 中删除 IEnumerable

    public class SessionModel { public int block { get;公共 class SessionModel { 公共 int 块 { 获取; set;放; } public string name { get; } 公共字符串名称 { 获取; set;放; } public string title { get; } 公共字符串标题 { 获取; set;放; } }

    } }

  3. Change thedeserialization part as follows改变反序列化部分如下

    var data = JsonConvert.DeserializeObject(line); var data = JsonConvert.DeserializeObject(line); foreach (SessionModel scenario in data.firstSession) { //Here you can get each sessionModel object Console.WriteLine(scenario.answer); foreach (SessionModel 场景 in data.firstSession) { //这里可以获取每个 sessionModel object Console.WriteLine(scenario.answer);

    } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM