locked
update json format in table RRS feed

  • Question

  • User932259438 posted

    Hi,

    I have table:

    tbl_Posts: ID, Post, Replies

    In column Replies I have format:

    {  "data": [{
        "ID": "1"
    ,    "AuthorUserID": "1236645",
        "Replies": "8e5s22t",
        "DeletePost": "no"
    
      }]
    }
    

    I want update DeletePost like that:

     var display = from t1 in db.tbl_Posts where t1.ID == 1 && t1.Replies==this need to get from Linq ID select t1;

    then I need to use

    display.FirstOrDefault.DeletePost="yes"
    db.SubmitChanges()

    So, First I need to find t1.ID==1 then, I need to find from coumn Replies ID from linq and update linq format: DeletePost="yes" or "No".

    Please help.

    Thursday, January 4, 2018 10:16 AM

Answers

  • User-832373396 posted

    Hi progy,

    Sir, based on your requirement, I tested a simple working example :

    (note: the only difference is that JSON string is in a text file and your JSON stored at  database)

    TextFile1.txt:

    {  
       "data":[  
          {  
             "ID":"1",
             "AuthorUserID":"1236645",
             "Replies":"8e5s22t",
             "DeletePost":"no"
          },
    	  {  
             "ID":"2",
             "AuthorUserID":"1236645",
             "Replies":"8e5s22t",
             "DeletePost":"no"
          }
       ]
    }

    Then code:

    string jsonStr= System.IO.File.ReadAllText(Path.Combine(@"C:\Users\v\Source\Repos\MVC","TextFile1.txt"));
    
       JObject josnObject= JObject.Parse(jsonStr);
    
    //josnObject["data"] and josnObject.First.First()  is the same effort:  {[  {    "ID": "1",    "AuthorUserID": "1236645",    "Replies": "8e5s22t",    "DeletePost": "no"  },  {    "ID": "2",    "AuthorUserID": "1236645",    "Replies": "8e5s22t",    "DeletePost": "no"  }]}
    
    var  json1=josnObject["data"] ;
    
    var json2 = josnObject.First.First().ToList().Where(a=>a["ID"].ToString()=="2").FirstOrDefault()

     Now json2:

    {{
    
      "ID": "2",
    
      "AuthorUserID": "1236645",
    
      "Replies": "8e5s22t",
    
      "DeletePost": "no"
    
    }}	 

    Then josnObject.First.First().ToList().Where(a=>a["ID"].ToString()=="2").FirstOrDefault()["DeletePost"]="yes"

     Now, it changed:   josnObject.ToString();

    So, full code:

    string jsonStr= System.IO.File.ReadAllText(Path.Combine(@"C:\Users\v\Source\Repos\MVC","TextFile1.txt"));
    
     JObject josnObject= JObject.Parse(jsonStr);
    
     josnObject.First.First().ToList().Where(a=>a["ID"].ToString()=="2").FirstOrDefault()["DeletePost"]="yes";
    
    string newJson=josnObject.ToString();

    newJson Result:

    {
      "data": [
        {
          "ID": "1",
          "AuthorUserID": "1236645",
          "Replies": "8e5s22t",
          "DeletePost": "no"
        },
        {
          "ID": "2",
          "AuthorUserID": "1236645",
          "Replies": "8e5s22t",
          "DeletePost": "yes"
          
        }
      ]
    }

    With regards, Angelina Jolie

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 5, 2018 7:14 AM