2

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"
          ]
    }
]
5
  • have you tried styling your text file as a json?? this is a simple text file (if you're really using the one above) so it's clear that JSON wont parse a TEXT file... Commented Dec 18, 2015 at 8:58
  • You need to make the class serializable. If it's serializable it's not obvious from the example provided. Commented Dec 18, 2015 at 9:02
  • Read a little about serialization first : msdn.microsoft.com/en-us/library/ms233843.aspx and for examples msdn.microsoft.com/en-us/library/ms172873.aspx Commented Dec 18, 2015 at 9:09
  • @ToniKostelac Can you please explain a little bit more. I get short text and images correctly but not the geocordinate Commented Dec 18, 2015 at 9:29
  • I think that @cramopy sums it up nicely in his answer, however I'd like to add that you'd, generally, want to mark your classes as serializable using the [Serializable] attribute. I think this might help with parsing text into double or any base type. Commented Dec 18, 2015 at 10:22

1 Answer 1

5

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:

  • Do you want to parse the Text you have and then save it as a json?
  • You wanna have the data in an array. Where do you have it? Where should the array be?

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");
Sign up to request clarification or add additional context in comments.

3 Comments

I have edited my question. I mentioned my json file below my code @cramopy
I only having problem in geocoordinate serialization
Now the problem arrise here- this.GeoCoordinates = File.ReadAllLines(startPath + @"\Latitude Longitude\" + name + ".json"); line. it shows Cannot implicitly convert type 'string[]' to 'Wikipedia_Image_Text_24_11_2015.GeoCoordinates'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.