0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

C#でJSONな記事補足

Last updated at Posted at 2020-02-02

うーん

参考にはなるけれどちょっと雑

C#:API呼び出し
https://blog.pie001.com/entry/2018/01/16/165318

これを実装ために追加すべきもの

nuget→DLL参照→usingの順で追加

nuget

・Newtonsoft.Json

Newtonsoft.Json
https://www.nuget.org/packages/Newtonsoft.Json/

DLL参照

以下手順
1.ソリューションエクスプローラー内の参照を右クリック
2.参照の追加(R)...
3.左側のアセンブリから
4.System.Web.Extensionsにチェック
5.OK

using

using System.Net;
using System.IO;
using System.Web.Script.Serialization;
using Newtonsoft.Json.Linq;

JSONパラメータの書き方

シリアライズするSerialize()メソッドを使った書き方

C#
var jsonParameter = new JavaScriptSerializer().Serialize(new
{
    name = "Hoge-San",
    email = "hogehoge@hoge.com",
    password = "h0gew0rd",
    detail_info = new
    {
        info1 = "hoge1",
        info2 = "hoge2"
    },
    hoge = "hogehoge",
});

このように記述することで、

JSON
{
    "name": "Hoge-San",
    "email": "hogehoge@hoge.com",
    "password": "h0gew0rd",
    "detail_info": {
        "info1": "hoge1",
        "info2": "hoge2"
    },
    "hoge":"hogehoge"
}

のようなJSONが生成され、jsonParameterに格納されます。

実際は改行・スペースなしの1行のstring型になるので、格納される正しい中身は、

jsonParameterの中身
{"name":"Hoge-San","email":"hogehoge@hoge.com","password":"h0gew0rd","detail_info":{"info1":"hoge1","info2":"hoge2"},"hoge":"hogehoge"}

のようになります。

おわり

記事にするならこれくらいはあった方が初心者にも熟練者にも親切だよね。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?