正在尝试从JSON行星API读取JSON数据
本文关键字:JSON 行星 API 读取 数据 | 更新日期: 2023-09-27 18:19:36
我正在尝试从这个JSON文件中读取数据http://www.astro-phys.com/api/de406/states?date=1000-1-20&body=mars日期读起来不错,但我想读:
[-168045229.22750974, 164411532.90034229, 80245103.265201837]
但由于数据没有名字,我不知道该怎么命名。这是我当前的代码
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;
using System.Windows.Forms;
namespace Planets
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
string filecontents;
string pagerequest = "http://www.astro-phys.com/api/de406/states?date=1000-1-20&bodies=mars";
WebRequest request = WebRequest.Create(@pagerequest);
WebResponse response = request.GetResponse();
Stream data = response.GetResponseStream();
string html = string.Empty;
using (StreamReader sr = new StreamReader(data))
{
filecontents = sr.ReadToEnd();
}
JavaScriptSerializer jsonSerialiser = new JavaScriptSerializer();
Results results = jsonSerialiser.Deserialize<Results>(filecontents);
richTextBox1.Text = (results.date);
Console.Write(results.date);
Console.ReadLine();
}
}
public class Results
{
public string date { get; set; }
public Position position { get; set; }
}
//public class Position
//{
// public int x { get; set; }
// public int y { get; set; }
//}
}
我是这方面的新手,如果有任何帮助,将不胜感激
感谢
您的Results
类应该类似于以下
public class Results
{
public string date { get; set; }
public IDictionary<string, IList<decimal[]>> results { get; set; }
public string unit { get; set; }
}
这允许在JSON中定义多个行星数据点。即,如果你打电话给http://www.astro-phys.com/api/de406/states?date=1000-1-20&bodies=mars,earth
,就会返回火星和地球等。
然后访问特定坐标
var coord1 = results.results["mars"][0][0]; // -168045229.22750974
var coord2 = results.results["mars"][0][1]; // 164411532.90034229
var coord3 = results.results["mars"][0][2]; // 80245103.265201837