none
i have got one json. i want to read it in following formate RRS feed

  • Question

  • hi , ihave following json in mongo db.

    i can get the rows from mongo db but then i have to do this in c# core.

    id:1

    /** 
    * Paste one or more documents here
    */
    {
        "nodeHierarchy": {
            "L1": "Mechanical"
        },
    
        "weightage": {
            "$numberDouble": "100"
        },
        "dateFrom": {
            "$date": {
                "$numberLong": "1581445800000"
            }
        },
        "dateTo": {
            "$date": {
                "$numberLong": "1582914600000"
            }
        },
        "WSID": "PRJ1",
        "scope": {
            "$numberDecimal": "1000"
        },
        "IsLeafNode": false
    }


    id:2

    /** 
    * Paste one or more documents here
    */
    {
        "nodeHierarchy": {
            "L1": "Mechanical",
            "L2": "Act1"
        },
        "weightage": {
            "$numberDouble": "100"
        },
        "dateFrom": {
            "$date": {
                "$numberLong": "1581445800000"
            }
        },
        "dateTo": {
            "$date": {
                "$numberLong": "1582914600000"
            }
        },
        "WSID": "PRJ1",
        "scope": {
            "$numberDecimal": "1000"
        },
        "IsLeafNode": true
    }

    i have many rows in above formate, but i have take only two rows for brevity,

    the first row is parent row which has "L1":mechenical, and second row is child of "mechenical" which is at "L2":"Act1"

    this heirarchy could be at n level.

    what i have to do is i have to get the child row the last row by filltering "IsleafNode"; true and then divide the weitage of child node with weitage of parent row, and then so on, till i get the top most parent.

    how can do it in c# core.

    yours sincerley

    Tuesday, February 18, 2020 9:52 AM

All replies

  • we are ready to change our json if u want to suggest some changes.
    Tuesday, February 18, 2020 9:55 AM
  • Show us the code you are using to convert this JSON to .NET objects. You aren't going to be able to do anything before the data is serialized into .NET code.

    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, February 18, 2020 3:01 PM
    Moderator
  • making object is fine, but is there any library for json in .net core c# like we have for xml to itrate it .

    like xmldocument and etc.

    Wednesday, February 19, 2020 8:09 AM

  • why do u want me to get things in c# object, because i notice i can itrate bjsondocument also and the get the value, not only that  mongodb c# linq library is also there . they do not need deserialisation.

    Wednesday, February 19, 2020 11:23 AM
  • .NET Core has the System.Text.Json assembly/namespace that introduces a first class JSON parser into .NET so that code can be moved away from JSON.NET. It is discussed here

    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, February 19, 2020 2:46 PM
    Moderator
  • You technically don't have to if you're comfortable working with the raw JObject (or equivalent) in the JSON library you're using. However as the rules get more complex you're going to find the code is harder to write/understand. For example changing the case of a field value is easy. Finding all objects with an ID of 10 and combining them into a single new object takes more work. 

    Furthermore the JSON parser you use is most likely going to create underlying objects for you anyway so you aren't saving any memory/runtime. Even XML parsers do this, they pretty much have to unless you use a streaming implementation. So instead of using generic objects that you have to write dictionary like (obj["Id"]) code or using dynamic (obj.id) which won't blow up until runtime it is generally cleaner to write the types you want to serialize to. It requires a little more code (defining the type) but the read/write logic is easier. Additionally you only need to define the properties you actually care about for reading. If you need to rewrite an existing JSON file then you would need to define all the properties so the file is properly re-written.

    In the end it is a tradeoff between how many types you define vs ease of writing the read/write code you need. It's up to you.


    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, February 19, 2020 2:52 PM
    Moderator