我尝试从此JSON文件中读取数据http://www.astro-phys.com/api/de406/states?date=1000-1-20&bodies=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; }
//}
}
我是全新的,任何帮助将不胜感激
由于
答案 0 :(得分:2)
您的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