转换给定json格式的模型

时间:2014-04-17 12:52:33

标签: javascript json

我有一个模型,其中的值以下列格式存储: -

Language-count=3
      [0]
         -ID="1"
         -Name="French"
      [1]
         -ID="2"
         -Name="English"
      [2]
         -ID="3"
         -Name="Hindi"
Titles-count=2
      [0]
         -ID="1"
         -Name="Video1"
      [1]
         -ID="2"
         -Name="Video2"
Countries-count=2
      [0]
         -ID="1"
         -Name="India"
      [1]
         -ID="2"
         -Name="USA"

我必须以给定的json格式转换此模型: -

var models = [
    {
        name: 'Language',
        values: [
            'English',
            'French',
            'Hindi'
        ]
    },
    {
        name: 'Title',
        values: [
            'Title 1',
            'Title 2'
        ]
    },
    {
        name: 'Countries',
        values: [
            'India',
            'UK'
        ]
    }
];

在上面的json格式中,我已经对语言,国家和标题的这些值进行了硬编码,但是我必须从我已经给出的上述模型中获取它。

我得到的json格式如下: -

{
    "ID": 1,
    "DealID": 1,
    "Title": "Position1",
    "Titles": [
        {
            "Icon": "hdtv",
            "Name": "\nWedding Bells & Farewells\n",
            "ID": 12
        },
        {
            "Icon": "hdtv",
            "Name": "Delta Farce",
            "ID": 5
        },
        {
            "Icon": "hdtv",
            "Name": "Doe B: Let Me Find",
            "ID": 9
        }
    ],
    "Episodes": [
        {
            "Icon": "episode",
            "Name": "Sparkle",
            "ID": 4
        },
        {
            "Icon": "episode",
            "Name": "Sparks Fly Out",
            "ID": 2
        },
        {
            "Icon": "episode",
            "Name": "Uploads by Filmi Gaane",
            "ID": 7
        }
    ],
    "Assets": [
        {
            "Icon": "file-o",
            "Name": "Best of Javed Akhtar - Jukebox 2 - Javed Akhtar Top 10 Hit Songs",
            "ID": 10
        },
        {
            "Icon": "file-o",
            "Name": "Ep 105 - Sin Say Shun Awards After Party additional image 1",
            "ID": 4
        },
        {
            "Icon": "file-o",
            "Name": "Ep 105 - Sin Say Shun Awards After Party box cover",
            "ID": 3
        }
    ],
    "Documents": [],
    "Languages": [
        {
            "Icon": "globe",
            "Name": "Albanian",
            "ID": 70
        },
        {
            "Icon": "globe",
            "Name": "Amharic",
            "ID": 96
        }
    ],
    "Territories": [],
    "Countries": [
        {
            "Icon": "globe",
            "Name": "Afghanistan",
            "ID": 2
        },
        {
            "Icon": "globe",
            "Name": "Albania",
            "ID": 3
        },
        {
            "Icon": "globe",
            "Name": "Algeria",
            "ID": 4
        }
    ],
    "Rights": [
        {
            "Icon": "leaf",
            "Name": "Ancillary",
            "ID": 23
        },
        {
            "Icon": "leaf",
            "Name": "Finshed Episode Rights",
            "ID": 20
        },
        {
            "Icon": "leaf",
            "Name": "Format Group - DO NOT USE",
            "ID": 63
        }
    ],
    "Contributors": [],
    "Transmissions": [],
    "Available": null
} 

2 个答案:

答案 0 :(得分:0)

最好编写一个简单的解析器并将您的数据类型转换为JSON - 这将允许您将来重用解析器,并将其轻松转换为其他数据类型。

您可以查看各种YAML解析器的灵感,它们将使用类似的技术来处理数据集的语言。

或者,如果您的数据格式始终采用此格式,并且不允许任意值嵌套,则可以创建“hack”并继续拆分。

答案 1 :(得分:-1)

列表人员=新的List();

        var client = new RestClient("your adres");
        var request = new RestRequest(Method.GET);
        request.AddHeader("Postman-Token", "2893de4a-457e-46a7e8efb025");
        request.AddHeader("cache-control", "no-cache");
        request.AddHeader("Content-Type", "application/json");
        request.AddHeader("token", "a23a80f7-3323-4594056");
        IRestResponse response = client.Execute(request);

        JObject deger = JObject.Parse(response.Content);
        var toplam = deger["data"]["data"].Count();


        string jenp = toplam.ToString();


        for (int i = 0; i < toplam; i++)
        {
            Personeller data = new Personeller();

            data.Adi = deger["data"]["data"][i]["adi"].ToString();
            data.Soyadi = deger["data"]["data"][i]["soyadi"].ToString();
            data.tckimlikno = (long)deger["data"]["data"][i]["tckimlikno"];



            personel.Add(data);
        }