Click here to Skip to main content
15,889,462 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have three txt files which contain the same Json data i want to read them all in c# class objects and overwrite all the objects from a textbox value in all the files at the same time

in short i have these fields common in all three text files
"time":"112429",
"code":"R123",
i want store their values in the c# object class from all three files at a time
and then overwrite the same object values supplied by a textbox value for all the attributes read by three different files

code to read single file data
C#
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
           {
               _orderRequest = (OrderRequest)JsonReader.ReadRecord(fs, _recordManager, TicketingRecordNames.OrderRequest);
           }


my object class
C#
public class OrderRequest
{
public string org;
public string raff;
public string time;
public string user;
public OrderRequest _orderRequest;
}


FILE 1 data
{  
   "org":"OrgTest",
   "raff":"Raffle1",
  "time":"112828",
   "user":"User1",
  
}


FILE 2 data
{  
   "org":"OrgTest",
   "raff":"Raffle1",
  "time":"112828",
   "user":"User111",
  
}


What I have tried:

i have tried newtonsoft json package serializing but not able to load multiple json text files to read, as well as cannot overwrite the object values of multiple files
Posted
Updated 5-Jul-18 2:44am
v3
Comments
F-ES Sitecore 4-Jul-18 6:53am    
You might have to either load the contents of the three files and merge them in a single string that can then be deserialised to a single object, or create three objects for each of the files to deserialise to, then simply copy the properties from the file-specific objects to your main object.
Member 13888852 4-Jul-18 7:28am    
but how can i combine three json text files data to one
F-ES Sitecore 4-Jul-18 8:28am    
Now I see the files have the same stricture. A property can only hold one value, if you plan on having all the data from all of the files in one class you're better using a List of some sort

public class MyData
{
public List<OrderRequest> Requests { get; set; }
}

Load each file into a new instance of OrderRequest and add it to the Requests property.
Graeme_Grant 4-Jul-18 7:43am    
Supply a sample of all 3 and the desired result.

One method is to load each JSON file and deserialize into separate lists, then merging all 3 into one. Now it's ready for your user to edit.
Member 13888852 4-Jul-18 8:02am    
can u please help with the code to supply multiple files
i have used this code to read single file
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
{
_orderRequest = (OrderRequest)JsonReader.ReadRecord(fs, _recordManager, TicketingRecordNames.OrderRequest);
}

1 solution

Just try to merge all the json files first and keep it in a single Json file and do Serialize and Deserialize that.

bject o1 = JObject.Parse(@"{
  'FirstName': 'John',
  'LastName': 'Smith',
  'Enabled': false,
  'Roles': [ 'User' ]
}");
JObject o2 = JObject.Parse(@"{
  'Enabled': true,
  'Roles': [ 'User', 'Admin' ]
}");

o1.Merge(o2, new JsonMergeSettings
{
    // union array values together to avoid duplicates
    MergeArrayHandling = MergeArrayHandling.Union
});

string json = o1.ToString();
// {
//   "FirstName": "John",
//   "LastName": "Smith",
//   "Enabled": true,
//   "Roles": [
//     "User",
//     "Admin"
//   ]
// }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900