JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999.
JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others.
JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others.
How to use c# read json file
Explore My Other Channel for More Cool and Valuable Insights
👉 Youtube Learn Tech Tips👉 Tiktok
👉 Facebook:These properties make JSON an ideal data-interchange language. JSON is built on two structures:
- A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
- An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.
C# is my favorite language, and this will help use do much projects will much uses.
This topic will be help you easy to read JSon file!
Explore My Other Channel for More Cool and Valuable Insights
👉 Youtube Learn Tech Tips👉 Tiktok
👉 Facebook:Json Format File:
{
"1": {
"result":
{
"ITEM_ID":"CNSK",
"NAME":"C\u1ea9m nang s\u1ef1 ki\u1ec7n",
"PRODUCT_CODE":"FS",
"ID_INGAME":"6,1,5572",
"TYPE":"1",
"START_TIME":null,
"END_TIME":null}
},
"0":1
}
-> json["1"].result.ITEM_ID = "CNSK" ;
-> json["1"].result.NAME = "Cẩm nang sự kiện";
-> json["1"].START_TIME = "";
https://learntechtips.com/mda/api/get_itemsIDbyName?NAME=C%E1%BA%A9m%20
Source Code:
private clsItem GetInfoItemByName(string name)
{
// phải có try catch vì khi trả dữ liệu về json[0]
try
{
clsItem item = new clsItem();
if (name.ToString() == "")
return null;
string strURL =
"https://learntechtips.com/mda/api/get_itemsIDbyName?NAME="
+ name + "&PRODUCT_CODE=FS";
var json = GetJSonFromServer(strURL);
if (json == null)
return null;
if (json["0"] == 0)
return null;
item.szItemID = json["1"].result.ITEM_ID;
item.szName = json["1"].result.NAME;
item.szIdIngame = json["1"].result.ID_INGAME;
item.szType = json["1"].result.TYPE;
item.szStartTime = json["1"].result.START_TIME;
item.szEndTime = json["1"].result.END_TIME;
item.szProductCode = json["1"].result.PRODUCT_CODE;
return item;
} catch (Exception ex)
{
return null;
}
}
private dynamic GetJSonFromServer(string strUrl)
{
try
{
WebClient wb = new WebClient();
var data = wb.DownloadString(strUrl);
var json = System.Web.Helpers.Json.Decode(data);
return json;
}
catch (Exception ex)
{
return null;
}
}
clsItem Class:
public class clsItem
{
private string _szItemID;
private string _szName;
private string _szProductCode;
private string _szidIngame;
private string _sztype;
private string _szStartTime;
private string _szEndTime;
public string szEndTime
{
get { return _szEndTime; }
set { _szEndTime = value; }
}
public string szStartTime
{
get { return _szStartTime; }
set { _szStartTime = value; }
}
public string szType
{
get { return _sztype; }
set { _sztype = value; }
}
public string szIdIngame
{
get { return _szidIngame; }
set { _szidIngame = value; }
}
public string szProductCode
{
get { return _szProductCode; }
set { _szProductCode = value; }
}
public string szName
{
get { return _szName; }
set { _szName = value; }
}
public string szItemID
{
get { return _szItemID; }
set { _szItemID = value; }
}
}
Thank you for reading this post. I hope you found it helpful and easy to follow. If you have any feedback or questions about
How to read Json File With C# in easy tutorial ,
please share them in the comments below. I would love to hear from you and discuss this topic further
✋✋✋✋
Webzone Tech Tips, all things Tech Tips for web development
- I am Zidane, See you next time soon ✋✋✋✋