【问题标题】:Is there a way to get a JSON data from a webpage and pass data into a textbox in c# winform?有没有办法从网页获取 JSON 数据并将数据传递到 c# winform 中的文本框?
【发布时间】:2020-06-29 11:03:51
【问题描述】:

我正在尝试从网页获取 json 数据

http://localhost/widgets/Screenshot_show/?pc_widget_output_method=JSON

在 c# 中将我的 windows 窗体应用程序放入文本框。那是 json 数据的链接,我正在尝试将时间属性放入文本框中,但没有得到它

这是尝试过的

        WebClient client = new WebClient();   //gets an object to access a website
        string download = client.DownloadString("http://localhost/widgets/Screenshot_show?pc_widget_output_method=JSON"); //download the json file into a string
        dynamic dobj = JsonConvert.DeserializeObject<dynamic>(download); //this function of newtonsoft here deserialize json data 

        string location = (string)dobj["JSON"]["sucess"];

        txtwebclient.Text = download;

但在尝试将时间属性检索到文本框时总是出错

JSON 示例

[
  {
    "name": "",
    "location": "/screenshots/",
    "time": 1584498343,
    "screenshots_id": "1584498343-0-33",
    "creation_time": 1584498343,
    "modified_time": "",
    "__user_id": 1583761141.0404,
    "__update_user_id": null,
    "__user_agent_id": "",
    "__ip": {
      "REMOTE_ADDR": "::1",
      "HTTP_CLIENT_IP": "",
      "HTTP_X_FORWARDED_FOR": ""
    }
  }
]

【问题讨论】:

  • 您遇到的错误是什么。通常你不能从其他方法访问 ui 线程。你能指定错误吗
  • 如果这是 JSON 内容,我怀疑 dobj["JSON"] 会返回任何内容。 JSON 中也没有可见的 success 属性。可以是:string location = (string)dobj["location"]

标签: c# html json winforms


【解决方案1】:

具有对象数组的 json。试试这个

var jArray = JArray.Parse(download);
var result = jArray[0]["location"];

【讨论】:

    猜你喜欢
    • 2022-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-20
    • 2020-01-04
    相关资源
    最近更新 更多