简体   繁体   中英

What is the best way to desirialize this string of json?

I was wondering how I could at best deserialize this json string. For doing that I am using Newtonsoft.json as a plugin with Xamarin.

I only need the parts that says "transaction" and the array in it "transactions" in a list.

{
  "id": 999,
  "transactions": [
    {
      "order": 1,
      "displayName": "01_lgn",
      "transaction": {
        "id": 7791,
        "name": "01_lgn",
        "description": null,
        "warning": 1,
        "poor": 2,
        "timeOut": 45,
        "tolerated": 3,
        "frustrated": 7,
        "state": 1,
        "includeInThroughputCalculation": true
      }
    }
    {
      "order": 2,
      "displayName": "02",
      "transaction": {
        "id": 7793,
        "name": "02",
        "description": null,
        "warning": 1,
        "poor": 2,
        "timeOut": 45,
        "tolerated": 3,
        "frustrated": 7,
        "state": 1,
        "includeInThroughputCalculation": true
      }
    }
  ],
  "defies": null,
  "state": 1,
  "reportDisplayName": "testSomething"
}

What I already have tried is to put it in a strongly typed class and then make a list of it.

public class testTransaction
    {
        [JsonProperty("id")]
        public int Id { get; set; }

        [JsonProperty("state")]
        public int State { get; set; }

        [JsonProperty("reportDisplayName")]
        public string ReportDisplayName { get; set; }

        [JsonProperty("transactions")]
        public List<testTransactions> testTransactions { get; set; }
    }

public class testTransactions
    {
        [JsonProperty("id")]
        public int Id { get; set; }

        [JsonProperty("name")]
        public string Name { get; set; }

        [JsonProperty("description")]
        public object Description { get; set; }

        [JsonProperty("warning")]
        public int Warning { get; set; }

        [JsonProperty("poor")]
        public int Poor { get; set; }

        [JsonProperty("timeOut")]
        public int TimeOut { get; set; }

        [JsonProperty("tolerated")]
        public int Tolerated { get; set; }

        [JsonProperty("frustrated")]
        public int Frustrated { get; set; }

        [JsonProperty("state")]
        public int State { get; set; }

        [JsonProperty("includeInThroughputCalculation")]
        public bool IncludeInThroughputCalculation { get; set; }
    }

But when I try to deserialize it in this way the "searchResult" is empty and I see that nothing is added to the list.

var bleh = jsonstring;
JObject parsedketenObject = JObject.Parse(bleh);
IList<JToken> jTokenResults1 = parsedketenObject;
IList<JToken> jTokenResults2 = parsedketenObject ["transactions"].Children ().ToList ();
IList<JToken> jTokenResults3 = parsedketenObject["transactions"][0]["transaction"].Children().ToList();
_Transactions_list = new List<testTransaction>();

foreach (JToken result in jTokenResults2)
{
     testTransaction searchResult = JsonConvert.DeserializeObject<testTransaction>(result.ToString());
     _Transactions_list.Add(searchResult);
}

Firstly, the JSON seems to be malformed, you are missing comma.

{
  "id": 999,
  "transactions": [
    {
      "order": 1,
      "displayName": "01_lgn",
      "transaction": {
        "id": 7791,
        "name": "01_lgn",
        "description": null,
        "warning": 1,
        "poor": 2,
        "timeOut": 45,
        "tolerated": 3,
        "frustrated": 7,
        "state": 1,
        "includeInThroughputCalculation": true
      }
    },   <-------- HERE
    {
      "order": 2,
      "displayName": "02",
      "transaction": {
        "id": 7793,
        "name": "02",
        "description": null,
        "warning": 1,
        "poor": 2,
        "timeOut": 45,
        "tolerated": 3,
        "frustrated": 7,
        "state": 1,
        "includeInThroughputCalculation": true
      }
    }
  ],
  "defies": null,
  "state": 1,
  "reportDisplayName": "testSomething"
}

Additionally, try these for your POCO instead:

public class TransactionDetails
{
    public int id { get; set; }
    public string name { get; set; }
    public object description { get; set; }
    public int warning { get; set; }
    public int poor { get; set; }
    public int timeOut { get; set; }
    public int tolerated { get; set; }
    public int frustrated { get; set; }
    public int state { get; set; }
    public bool includeInThroughputCalculation { get; set; }
}

public class Transaction
{
    public int order { get; set; }
    public string displayName { get; set; }
    public TransactionDetails transaction { get; set; }
}

public class RootObject
{
    public int id { get; set; }
    public List<Transaction> transactions { get; set; }
    public object defies { get; set; }
    public int state { get; set; }
    public string reportDisplayName { get; set; }
}

Then you can use

var x = JsonConvert.DeserializeObject<RootObject>(blah);

x will contain transactions , which is already a list.

This method is not strongly typed , but it will work:

var jsonString = GetTheJson(); // however you get your json

dynamic jsonObject = JsonConvert.DeserializeObject(jsonString);
foreach (var txn in jsonObject.transactions)
{
    Console.WriteLine("{0} {1} {2}", txn.order, txn.displayName, txn.transaction.id);
}

The best way to serialize/deserialize a json string to json object is using Google Gson library. You should create DTO files and use its type to serialize the string. The documentation can be found here: https://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/Gson.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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