-1

I have a json file as follows:

{
  "container" : {
    "cans1" : 
    [
      {
        "name" : "sub",
        "ids" : 
        [
          "123"
        ]
      },
      {
        "name" : "Fav",
        "ids" : 
        [
          "1245","234"
        ]
      },
      {
        "name" : "test",
        "ids" : 
        [
          "DOC12","DOC1234"
        ]
      }
    ],
    "ids" : 
    [
      "1211","11123122"
    ],
"cans2" : 
    [
      {
        "name" : "sub1",
        "ids" : 
        [
          "123"
        ]
      }
     ],
     "ids" : 
    [
      "121","11123"
    ]

}

I want to fetch name values sub,fav,test and ids for each cans in this json file using c#

Guru Stron
  • 102,774
  • 10
  • 95
  • 132
harshita
  • 3
  • 1

1 Answers1

0

Install nuget Newtonsoft.Json. Create next hierarchy:

using System;
using System.Collections.Generic;

using System.Globalization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

public class MyClass
{
    [JsonProperty("container")]
    public Container Container { get; set; }
}

public class Container
{
    [JsonProperty("cans1")]
    public Cans[] Cans1 { get; set; }

    [JsonProperty("ids")]
    [JsonConverter(typeof(DecodeArrayConverter))]
    public long[] Ids { get; set; }

    [JsonProperty("cans2")]
    public Cans[] Cans2 { get; set; }
}

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

    [JsonProperty("ids")]
    public string[] Ids { get; set; }
}

And then

 JsonConvert.DeserializeObject<MyClass>(yourJsonString);

UPD

Based on comment, try this:

var des = JsonConvert.DeserializeObject<MyClass>(t);

foreach(var arr in des.Container.Where(r => r.Key.StartsWith("cans")))
{

    Console.WriteLine($"{arr.Key}");
    foreach(var elem in arr.Value)
    {
        Console.WriteLine($"    {elem.Value<string>("name")}");
    }
}

public class MyClass
{
    [JsonProperty("container")]
    public Dictionary<string, JArray> Container { get; set; }
}
Guru Stron
  • 102,774
  • 10
  • 95
  • 132
  • And what prevents you from it? – Guru Stron May 01 '20 at 16:27
  • Json string is dynamic at runtime, sometimes there will be two cans array inside sometimes there may not be a can object at all in that case how will iterate through dynamic json string and fetch values of cans name and other properties – harshita May 01 '20 at 16:45
  • it works perfect thank you so much, being new to c# I had no idea over this dictionaries and arrays, it helped me a lot. – harshita May 01 '20 at 18:19
  • I have one more requirement that is containers may also vary there can be different containers like different cans containing same structure of json how will iterate in this case? – harshita May 01 '20 at 19:17
  • { "steelcontainer" : { "cans1" : [ { "name" : "Fav", "ids" : [ "1245","234" ] } ], "ids" : [ "1211","11123122" ], "cans2" : [ { "name" : "sub1", "ids" : [ "123" ] } ], "ids" : [ "121","11123" ] }, "glasscontainer" : { "cans5" : [ { "name" : "sub", "ids" : [ "123" ] }] } } – harshita May 02 '20 at 16:28
  • earlier inside container there were cans now json has multiple containers and cans for this how will get details of all container and its cans number of containers as well as cans will vary in the json – harshita May 02 '20 at 16:30
  • @harshita use the same approach, you can deserialize to nested Dictionaries or just "plain" `JObject`(instead of `MyClass`) . – Guru Stron May 02 '20 at 19:39