Reputation: 67
I have generated two text file and jpeg file from wikipedia. Now I want to serialize all this file in a json format. 1st I generated a short text file, 2nd I generated Latitude and longitude for that location and third is image file as hash code. Image and shorttext file is working perfectly. But I am having problem with latitude and longitude file. My text file is look like this with new line
Lat:54.33333333
Lon:10.13333333
I want to get this in an array. Now My json class which I am using to create json for all object is -
public class GeoCoordinates
{
public double Longitude { get; set; }
public double Latitude { get; set; }
}
public class POIData
{
public string ShortText { get; set; }
public GeoCoordinates GeoCoordinates { get; set; }
public List<string> Images { get; set; }
public string ToJson()
{
string json = JsonConvert.SerializeObject(this, Formatting.Indented);
return json;
}
public void FromJson(string json)
{
dynamic poi = JsonConvert.DeserializeObject<POIData>(json);
ShortText = poi.ShortText;
Images = poi.Images;
GeoCoordinates = poi.GeoCordinates;
}
// This method will create json file for Neuschwanstein_Castle
public void ToJsonForLocation(string name)
{
var startPath = Application.StartupPath;
string folderName = Path.Combine(startPath, "Text_ImageWithHash");
System.IO.Directory.CreateDirectory(folderName);
string fileName = name + ".json";
var path = Path.Combine(folderName, fileName);
var Jpeg_File = new DirectoryInfo(startPath + @"\Image\" + name).GetFiles("*.jpg");
this.ShortText = File.ReadAllText(startPath + @"\Short Text\" + name+".json");
this.GeoCoordinates = File.ReadAllLines(startPath + @"\Latitude Longitude\" + name + ".json");
this.Images = new List<string> { Jpeg_File[0].Name, Jpeg_File[1].Name };
string json = ToJson();
File.WriteAllText(path, json);
}
}
I am trying to get geocordinate in an array.But not working at all.
my json file look like this
[
{
"ShortText": "Kiel is the capital and most &tldr; from the wedge form of its bay ",
"GeoCordinates": null,
"Images": [
"9B9C2047.jpg",
"CAD72F59.jpg"
]
}
]
Upvotes: 2
Views: 6546
Reputation: 3497
Your problem is the format you store your data in. What you currently have is no json. A example json from your class would look like this:
[
{
"ShortText": "ShortText9daba9d8-1e0c-4b70-9f69-016332b1b53a",
"GeoCordinates": [
"11b2a991-0165-4155-8207-f256d2486ddd",
"9abd9c64-f0f2-47fb-a692-c6daae15a5bc",
"bfd2edd5-6f37-45ae-abf9-350cb20a5f5f"
],
"Images": [
"03425202-d953-44c5-bc50-bffdd4e7f605",
"dd6d0a5f-8e93-4793-82b7-4aba3515993c",
"8d554f56-af65-4ef3-b06a-88d2aab647e1"
]
}
]
You see the difference? Now when you replace the content of mine json sample with the samples you want, save that to the file and you can deserialize it.
After writing this, I shortly read your question again I have a pair of questions:
EDIT:
Your class should look like this:
//using System.Collections.Generic;
public class GeoCoordinates
{
public double Longitude { get; set; }
public double Latitude { get; set; }
}
public class POIData
{
public string ShortText { get; set; }
public GeoCoordinates GeoCoordinates { get; set; }
public List<string> Images { get; set; }
}
which results in this json:
[
{
"ShortText": "ShortText41cca2ce-b139-458e-b20c-c549ba0e0163",
"GeoCoordinates": {
"Longitude": 10.13333333,
"Latitude": 54.33333333
},
"Images": [
"9B9C2047.jpg",
"CAD72F59.jpg"
]
}
]
EDIT 2:
public GeoCoordinates GeosFromString(string path)
{
string[] lines = File.ReadAllLines(path);
GoeCoordinates gc = new GeoCoordinates();
gc.Latitude = Double.Parse(lines[0].Substring(4)); // this removes 'Lat:' in front of the string
gc.Longitude = Double.Parse(lines[1].Substring(4)); // this removes 'Lon:' in front of the string
return gc;
}
Usage:
this.GeoCoordinates = GeosFromString(startPath + @"\Latitude Longitude\" + name + ".json");
Upvotes: 5