今天根据需求要从百度API中取出一些数据,这些操作包括:将坐标转换成百度坐标,根据转换的百度坐标进行特定的查询。
有需求的收藏下,免得下次手写浪费时间。
涉及到的操作有:JSON格式的字符解析,HTTP请求和获得请求数据,文件流的写入和读出等等。
奉上源代码,共享:
首先是入口函数:
static void Main(string[] args)
{
Console.WriteLine("坐标转换,信息查询开始......");
string oFile = "C:\\Users\\MisterHu\\Desktop\\点加属性\\聚类点.txt";
string iFile = "C:\\Users\\MisterHu\\Desktop\\点加属性\\result.txt";
List<string> tCoords = new List<string>();//存转换前坐标
List<string> cvtCoords = new List<string>();//存转换后坐标
List<string> rstQuery = new List<string>();//存查询结果
string sInfo; //临时字符变量
StreamReader sr = new StreamReader(oFile, Encoding.Default);
while ((sInfo = sr.ReadLine()) != null)
{
tCoords.Add(sInfo.Substring(sInfo.IndexOf(',')+1));
}
sr.Close();
//转换成百度坐标,然后存放到List中
BaiDuAPI.CoordConvert(tCoords, cvtCoords);
//从List中取出每个坐标,然后调用API
BaiDuAPI.PlaceAPI(cvtCoords, rstQuery);
//写入文件。
String strLine = null;
for (int i = 1; i <= 1000;++i )
{
strLine += i.ToString() + "," + tCoords[i - 1] + rstQuery[i - 1] + "\r\n";
}
FileStream.WriteInFile(strLine, iFile);
Console.WriteLine("转换,获取,完成!");
}
字符串输入输出到文件:
//输入输出字符到文件中
public static class FileStream
{
public static void ReadFormFile(string path)
{
}
public static void WriteInFile(string content,string path)
{
string iFile = "D:\\temp.txt";
if (path == null) path = iFile;
StreamWriter sw = new StreamWriter(path);
sw.Write(content);