简体   繁体   中英

convert json to another structure

I am trying to convert the structure by having a list with different category and type and combine them to a diff structure (group by type name).

I have put the full structure inside the jsfiddle here

The plan is to understand how to do it using javascript or c#.

var fromJson = [
{
    "type" : "Chicken",
  "total" : 1,
  "category" : "healthy"
},
{
    "type" : "Pig",
  "total" : 10,
  "category" : "healthy"
},
{
    "type" : "Pig",
  "total" : 5,
  "category" : "unhealthy"
},
{
    "type" : "Cow",
  "total" : 15,
  "category" : "healthy"
}
];

To

var final_out = [
    {
    "type" : "chicken",
    "healthy" : 1,
    "unhealthy" : 0
  },
  {
    "type" : "Pig",
    "healthy" : 10,
    "unhealthy" : 5
  },
  {
    "type" : "Cow",
    "healthy" : 15,
    "unhealthy" : 0
  }
]

Try:

public class RootObject
{
  List<RootType> rootType;
}    

public class RootType
{
  public int healthy { get; set; }
  public int unhealthy { get; set; }
}

or just:

public class RootObject
{
  public string type { get; set; }
  public int healthy { get; set; }
  public int unhealthy { get; set; }
}

So instead of modifying the collection, Try removing the actual object and add it back again with updated data. I use Linq for this.

Below is a small function that i put together to do this.

public string ReturnFinalOutput(string InputJson)
{
    List<fromJson> input = JsonConvert.DeserializeObject<List<fromJson>>(InputJson);
    List<final_out> output = new List<final_out>();
    foreach (fromJson j in input)
    {
        final_out o = new final_out();
        var finalout = output.FirstOrDefault<final_out>(a => a.type == j.type);
        if (finalout == null)
        {
            o.type = j.type;
            if (j.category == "healthy")
            {
                o.healthy = o.healthy + 1;
            }
            else if (j.category == "unhealthy")
            {
                o.unhealthy = o.unhealthy + 1;
            }
            output.Add(o);
        }
        else
        {
            output.Remove(finalout);
            if (j.category == "healthy")
            {
                finalout.healthy = finalout.healthy + 1;
            }
            else if (j.category == "unhealthy")
            {
                finalout.unhealthy = finalout.unhealthy + 1;
            }
            output.Add(finalout);
        }
    }
    return JsonConvert.SerializeObject(output);
}

and Below are my BaseClasses

public class fromJson
{
    public string type { get; set; }
    public int total { get; set; }
    public string category { get; set; }
}

public class final_out
{
    public string type { get; set; }
    public int healthy { get; set; } = 0;
    public int unhealthy { get; set; } = 0;
}

you can call this as

string final_out = ReturnFinalOutput(fromJson); // fromJson is your InputJson.

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