简体   繁体   中英

how to convert a JSON structure to another and add a extra field in to it

i have json data, which is coming from an API. the structure of json is like this

{
"data": [
    {
        "nameid": "s_10",
        "size": "6.46",
        "name": "abc",
        "children": [
            {
                "nameid": "i_101010",
                "size": "8.84",
                "name": "bcd",
                "children": [
                    {
                        "nameid": "si_10101010",
                        "size": "4.00",
                        "name": "efg",
                        "children": [
                            {
                                "nameid": "c_3273",
                                "size": 4,
                                "name": "ttt",
                            }
                        ]
                    },
                    {
                        "nameid": "si_10101020",
                        "size": "13.67",
                        "name": "sss",
                        "children": [
                            {
                                "nameid": "c_4450",
                                "size": 1,
                                "name": "rrr",
                            },
                            {
                                "nameid": "c_551",

                                "size": 17,
                                "name": "ddd",

                            },

i need to convert it to something like the below structure

  {
"id": 1,
"parentId": "NULL",
"name": "Root",
"size": 5,
"children": [
{
"id": 10,
"parentId": "1",
"name": "En",
"size": 1,
"children": [
 {
  "id": 1010,
  "parentId": "10",
  "name": "Eee",
  "size": 1,
  "children": [
   {
    "id": 101010,
    "parentId": "1010",
    "name": "Enh",
    "size": 5,
    "children": [
     {
      "id": 10101010,
      "parentId": "101010",
      "name": "Ooo",
      "size": 5
     },

so in short, i need to add that field parent id to the first JSON

note: these are partial data, so json may not be a valid one.

If your source and destination object are Typed Class (eg public class FamilyMembers )

Automapper is the best way to achieve this.

http://automapper.org/

Let's say your source 'data" is class Families . You can map Families to FamilyMembers

//specify source, destination
Mapper.CreateMap<Families, FamilyMembers>(); 

//get source data
Families families = GetFamiliesFromAPI(); 

//map each properties of  families to familymembers
FamilyMembers familymembers = Mapper.Map<Families, FamilyMembers>(families); 

ShowFamilyMembersInDataGrid(familymembers);

If you don't want to use Automapper, then go with LINQ

Families families = GetFamiliesFromAPI(); 
families.Select(f => f, new FamilyMembers() {name = f.Name, someProperty = f.someProperty }

Since you have nested JSON object you need to learn how to handle such as "children" member in your source data and map if via LINQ accordingly

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