Koyi yadda ake canza CSV da kyau zuwa tsarin JSON.
CSV (Wakafi-Raba Ƙimar) tsarin fayil ne da ake amfani da shi sosai don adanawa da musayar bayanan tabular. Yayin da CSV tsari ne mai sauƙi kuma mai sauƙin amfani, ba koyaushe shine mafi kyawun tsari don aikace-aikacen yanar gizo ba. JSON (JavaScript Object Notation) tsari ne mai sauƙi na musanyar bayanai wanda ke da sauƙin karantawa da rubutu ga ɗan adam, kuma mai sauƙi ga injuna don tantancewa da samarwa. Ana ƙara amfani da JSON azaman tsarin bayanai don APIs na yanar gizo, yana mai da shi mashahurin zaɓi ga masu haɓakawa. Mayar da fayilolin CSV zuwa tsarin JSON na iya inganta ingantaccen sarrafa bayanai da ba da damar yin amfani da bayanai ta aikace-aikacen yanar gizo ta hanyar da ta fi dacewa da mai amfani. A cikin wannan koyawa, za mu jagorance ku ta hanyar canza fayilolin CSV zuwa tsarin JSON ta amfani da C# .NET.
- API ɗin Canjin CSV zuwa JSON
- Canza CSV zuwa JSON ta amfani da C#
- CSV akan layi zuwa JSON ta amfani da Umarnin CURL
API ɗin Canjin CSV zuwa JSON
Aspose.Cells Cloud SDK don NET yana ba da fa’idodi da yawa waɗanda suka mai da shi ingantaccen kayan aiki don juyar da CSV zuwa tsarin JSON. Da farko dai, API ne na tushen girgije, wanda ke nufin cewa babu buƙatar shigar da kowace software ko ɗakin karatu akan injin ku na gida. Wannan yana ba da sauƙin farawa tare da, kuma yana kawar da buƙatar saiti mai rikitarwa da daidaitawa. Bugu da ƙari, Aspose.Cells Cloud SDK don NET yana da ƙima sosai kuma yana iya ɗaukar manyan kundin bayanai, yana sa ya dace da aikace-aikacen matakin kasuwanci. Tsarin jujjuyawar yana da sauri, abin dogaro, kuma yana samar da ingantaccen fitarwa na JSON wanda ke da sauƙin rarrabawa da amfani da aikace-aikacen yanar gizo.
Za mu fara da ƙara bayanin SDK a cikin aikace-aikacen mu ta mai sarrafa fakitin NuGet. Bincika “Aspose.Cells-Cloud” kuma danna maɓallin Ƙara Kunshin. Na biyu, idan baku da asusu akan Cloud Dashboard, da fatan za a ƙirƙiri asusun kyauta ta amfani da ingantaccen adireshin imel kuma sami keɓaɓɓen takaddun shaidarku.
Canza CSV zuwa JSON ta amfani da C#
Domin yin canjin daftarin aiki, muna da kiran API guda uku don cika wannan buƙatu.
- GetWorkbook - Get input CSV from Cloud storage. After conversion, save output to cloud storage.
- PutConvertWorkbook - Converts CSV file to other formats from request content.
- PostWorkbookAjiyeAs - Saves CSV file as other formats file to storage.
A cikin snippet lambar mai zuwa, za mu yi amfani da kiran GetWorkbook API wanda ke loda shigar da CSV daga ma’ajiyar gajimare, ya canza shi zuwa JSON sannan ya adana fitarwa zuwa ma’ajiyar girgije iri ɗaya.
// Don cikakkun misalai da fayilolin bayanai, da fatan za a je zuwa
https://github.com/aspose-cells-cloud/aspose-cells-cloud-dotnet/
// Samu bayanan abokin ciniki daga https://dashboard.aspose.cloud/
string clientSecret = "4d84d5f6584160cbd91dba1fe145db14";
string clientID = "bb959721-5780-4be6-be35-ff5c3a6aa4a2";
// ƙirƙiri misalin CellsApi yayin wuce ClientID da ClientSecret
CellsApi cellsInstance = new CellsApi(clientID, clientSecret);
// Littafin aikin farko na Excle akan tuƙi
string input_CSV = "input.csv";
// sunan littafin aikin Excel na biyu
string resultant_File = "output.json";
try
{
// loda CSV zuwa ma'ajiyar gajimare
cellsInstance.UploadFile(input_CSV, File.OpenRead(input_CSV));
// fara aikin juyawa
var response = cellsInstance.CellsWorkbookGetWorkbook(input_CSV, null, format:"JSON", null, outPath:resultant_File);
// buga saƙon nasara idan haɗin kai ya yi nasara
if (response != null && response.Equals("OK"))
{
Console.WriteLine("CSV to JSON converted successfully !");
Console.ReadKey();
}
}
catch (Exception ex)
{
Console.WriteLine("error:" + ex.Message + "\n" + ex.StackTrace);
}
Bari mu haɓaka fahimtarmu game da snippet na sama:
CellsApi cellsInstance = new CellsApi(clientID, clientSecret);
Ƙirƙiri wani abu na CellsApi yayin ƙaddamar da bayanan abokin ciniki azaman muhawara.
cellsInstance.UploadFile(input_CSV, File.OpenRead(input_CSV));
Loda shigar da CSV zuwa ma’ajiyar gajimare.
var response = cellsInstance.CellsWorkbookGetWorkbook(input_CSV, null, format:"JSON", null, outPath:resultant_File);
Fara aikin CSV zuwa JSON. Bayan nasarar nasarar juyawa, ana adana fitarwar fayil na JSON zuwa ma’ajin gajimare.
Za a iya sauke samfurin CSV da aka yi amfani da shi a cikin misalin da ke sama daga input.csv kuma za a iya sauke fayil ɗin JSON na sakamakon daga output.json.
CSV akan layi zuwa JSON ta amfani da Umarnin CURL
Canza CSV zuwa JSON ta amfani da umarnin cURL da REST API yana ba da fa’idodi da yawa. Da farko dai, hanya ce mai sauƙi kuma mai sauƙin amfani wacce ba ta buƙatar ƙarin software ko ɗakunan karatu da za a shigar. Bugu da ƙari, umarnin cURL da REST API sun kasance masu zaman kansu na dandamali, wanda ke nufin cewa ana iya amfani da hanya iri ɗaya akan kowane tsarin aiki ko yaren shirye-shirye wanda ke goyan bayan umarnin cURL da REST API. Wannan ya sa ya zama kyakkyawan bayani ga masu haɓakawa waɗanda ke aiki tare da dandamali da yawa da harsunan shirye-shirye.
Yanzu a cikin wannan sashe, za mu koyi matakai kan yadda ake canza CSV zuwa JSON akan layi ta amfani da umarnin cURL. Don haka mataki na farko shine samar da alamar samun damar JWT dangane da shaidar abokin ciniki:
curl -v "https://api.aspose.cloud/connect/token" \
-X POST \
-d "grant_type=client_credentials&client_id=bb959721-5780-4be6-be35-ff5c3a6aa4a2&client_secret=4d84d5f6584160cbd91dba1fe145db14" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"
Da zarar mun sami alamar JWT, za mu kira GetWorkbook API don canza CSV zuwa JSON akan layi. Da fatan za a aiwatar da umarni mai zuwa:
curl -v -X GET "https://api.aspose.cloud/v3.0/cells/input.csv?format=JSON&isAutoFit=false&onlySaveTable=false&outPath=resultant.json&checkExcelRestriction=true" \
-H "accept: application/json" \
-H "<JWT Token>"
Karshen Magana
A cikin wannan koyawa, mun bincika hanyoyi biyu don canza fayilolin CSV zuwa tsarin JSON - ta amfani da umarnin C# .NET da cURL tare da REST API. Duk hanyoyin biyu suna da fa’idodin su, kuma zaɓin ƙarshe ya dogara da takamaiman bukatun aikin ku. Tare da C# .NET, mun sami damar yin amfani da Aspose.Cells Cloud SDK don canza fayilolin CSV da kyau zuwa tsarin JSON akan layi, yayin da umarni na CURL da REST API suka ba da hanya mai sauƙi da dandamali mai zaman kanta wanda ke buƙatar ƙarin software ko ɗakin karatu. Ko da kuwa hanyar da kuka zaɓa, canza fayilolin CSV zuwa tsarin JSON na iya kawo inganci da abokantakar mai amfani ga aikace-aikacen yanar gizon ku, yana ba ku damar daidaita sarrafa bayanai da sarrafa bayanai.
Hanyoyin haɗi masu amfani
Abubuwan da aka Shawarar
Da fatan za a ziyarci hanyoyin haɗin yanar gizon don ƙarin koyo game da: