开发者

JSON.NET Add/edit children

开发者 https://www.devze.com 2023-04-04 19:57 出处:网络
I\'m currently trying to read a JSON document from the webserver (it\'s a physical json file) and check for changes against my database. In other words, I need to sync the json document and the databa

I'm currently trying to read a JSON document from the webserver (it's a physical json file) and check for changes against my database. In other words, I need to sync the json document and the database every night or so.

For this I'm using JSON.NET, which I believe is the way to go when working with JSON in C# (?) The .NET platform is version 3.5.

So far I am able to read the document and get values from it.

My JSON looks like this:

{"members":[

{"member":
    {
        "id":"4282",
        "status":"1931",
        "aktiv":"1",
        "firmanavn":"firmname1",
        "firmaUrl":"www.firmsite.com",
        "firmaUrlAlternativ":"",
        "firmaSidstKontrolleretDato":"30-08-2010",
        "firmaGodkendelsesDato":"07-03-2002"
    }
},
{"member":
    {
        "id":"4283",
        "status":"1931",
        "aktiv":"1",
        "firmanavn":"firmname2",
        "firmaUrl":"www.firmsite.com",
        "firmaUrlAlternativ":"",
        "firmaSidstKontrolleretDato":"30-08-2010",
        "firmaGodkendelsesDato":"18-12-2000"
    }
}, .... and so on
]}

And my C# code is as follows:

using (TextReader reader = File.OpenText(Server.MapPath("~/members.json")))
    {
        JsonTextReader jsonreader = new JsonTextReader(reader);
        var o = JObject.ReadFrom(jsonreader);
        jsonreader.Close();
        JObject obj = JObject.Parse(o.ToString());

        JArray items = (JArray)obj["members"];
        var members = Member.GetAllAsList();

        JObject item;
        JToken token;

        List<string> currentIds = new List<string>();
        for (int i = 0; i < items.Count; i++)
        {
            item = (JObject)items[i];
            token = item.First;

            while (token != null)
            {
                //ltTest.Text += ((JProperty)token).Value["firmanavn"].ToString() + System.Environment.NewLine;
                currentIds.Add(((JProperty)token).Value["id"].ToString());
                token = token.Next;
            }
        }

        foreach (var member开发者_开发知识库 in members)
        {
            if (!currentIds.Contains(member.Id.ToString()))
            {
                ltTest.Text += member.Text;
            }
        }
    }

So basically, there's three scenarios now:

  • A members information has been changed in the database: I need to find the member in the JSON document and update the corresponding fields

  • A member has been deleted from the database: I need to find the member in the JSON document and delete it.

  • A member has been added to the database: I need to add that member to the JSON document.

So, is anyone familiar with JSON.NET to give a hint (or perhaps some code-examples?) on how to achieve this? I barely made it to actually read the json document from the server, so this seems like an uphill battle for me at the moment.


Json.net has a thing called Linq-to-JSON, which according to the website is:

LINQ to JSON isn't a LINQ provider but rather an API for working with JSON objects. The API has been designed with LINQ in mind to enable the quick creation and querying of JSON objects.

So it supports LINQ querying which, I haven't used Linq-to-JSON but i am sure, will fulfill all three of your requirements.

For reference and how-tos please follow these links: http://james.newtonking.com/archive/2008/02/11/linq-to-json-beta.aspx http://james.newtonking.com/projects/json/help/


To fulfill your requirements, I would suggest the following steps:

  • Parse your JSON feed into easy to use JSON Object just like you did
  • In order to get your requirements fulfilled, the easiest is probably doing table to table comparison using SQL.
    • Create a staging tables to put the JSON data in
    • Convert the data in JSON feed into Relational form. In your case, it is pretty straightforward of creating table MEMBER with the 8 columns

From there, you issue SQL queries that are matching with your three cases which is:

  • A members information has been changed in the database: I need to find the member in the JSON document and update the corresponding fields

    In order to do this, issue a SQL Query that compare each field, use the resultset to update the JSON

  • A member has been deleted from the database: I need to find the member in the JSON document and delete it.

    Issue a SQL Query doing comparison using NOT IN comparator from original tables comparing with the Member tables that you just created. From here you will get collection of IDs from the result set that you could use to delete the appropriate JSON Object

  • A member has been added to the database: I need to add that member to the JSON document.

    Do the reverse of the previous steps. Issue a SQL Query comparing data that is in the database but not in the Member table. Use the resultset to add to the JSON Object

At the end of the process, you would have the JSON Object that is representing the sync between the two

0

精彩评论

暂无评论...
验证码 换一张
取 消