I would like to know how to make the json data generated by C# available in javascript.

Asked 1 years ago, Updated 1 years ago, 349 views

Regarding the presentation code, I would like to refer to the json string of let json in the source code below in the form of arr.name, but why is it displayed as undefined?I checked the reference site, and they said they would do it as follows.

The following source code uses C#WebView to send json data to javascript and draw using html.

GetSendJsonData_Post(JsonData.Postpost)is the process of making its C# class json data, and the json code is the processed json data.

The js source code is the test code to verify the behavior of that json.

The json code processed by C# can also be used on the javascript side.I would like to know how to put class data in the json format.

JsonSerializer.Serialize to make classes json
Get json value by converting it to json form with reference to reference site

json translation:https://cpoint-lab.co.jp/article/201801/1228/
JSON.stringify():https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
What is Json?https://developer.mozilla.org/ja/docs/Learn/JavaScript/Objects/JSON

 undefined
Main.js:8 "{name:sampleName [email protected], icon_url:https://media.mstdn.jp, accountPage:https://google.com, content:sampletext}"
Main.js:9 {name:sampleName [email protected], icon_url:https://media.mstdn.jp, accountPage:https://google.com, content:sampletext}
 {"name": "XXXXXXXX", "icon_url": "url", "accountPage": "url", "content": "AAAA"}

        /*##################################################################################################################
        * Get json of post data
        ###################################################################################################################*/
        static bool b = false;

        private string GetSendJsonData_Post(JsonData.Postpost)
        {
            string name=post.account.display_name+"+post.account.username+"@"+account.client.instance;
            // Console.WriteLine(name);
            // string icon_url = post.account.avatar;
            string icon_url = "url";
            string accountPage="url";
            // string accountPage=post.account.url;
            string content=post.content;

            varjson=new JsonData.SendData.Post(name, icon_url, accountPage, content);
            

            
            string jsonString = JsonSerializer.Serialize(json);
            if(b==false)
            {
                  Console.WriteLine(jsonString);
                // Console.WriteLine(json);

                b = true;
            }
            return jsonString;
        }

let json="{name:[email protected],icon_url:https://media.mstdn.jp,accountPage:https://google.com,content:sampletext}";


letarr = JSON.stringify(json);
var = JSON.parse(arr);

console.log(ar[0].name);
console.log(arr);
console.log(ar);

    

javascript

2022-12-02 02:10

1 Answers

From the questionnaire

json

 {"name": "XXXXXXXX", "icon_url": "url", "accountPage": "url", "content": "AAAA"}

Source Code

let json="{name:[email protected],icon_url:https://media.mstdn.jp,accountPage:https://google.com,content:sampletext}";

The string is completely different from the JSON generated by C#. " disappears or spaces are added.

I would like to know how to make the json data generated by C# available in javascript.

You must first copy the string exactly.


2022-12-02 03:31

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.