Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

JSon C# extraction

Discussion in 'Scripting' started by EdgarCarrera, Jan 6, 2017.

  1. EdgarCarrera

    EdgarCarrera

    Joined:
    Apr 21, 2014
    Posts:
    255
    Hello guys i need help extracting this into unity :

    https://itunes.apple.com/us/rss/topfreeapplications/limit=20/json

    can you help me ?

    my class
    Code (CSharp):
    1. public class Feed
    2.     {
    3.         public Author author;
    4.  
    5.         public class Author
    6.         {
    7.  
    8.             public Name name;
    9.  
    10.             public class Name
    11.             {
    12.                 public String label;
    13.             }
    14.  
    15.             public Uri uri;
    16.  
    17.             public class Uri
    18.             {
    19.                 public String label;
    20.             }
    21.         }
    22.  
    23.         public List<Entry> entry;
    24.  
    25.         public class Entry
    26.         {
    27.             [FormerlySerializedAs("im:name")]
    28.             public Name name;
    29.  
    30.             public class Name
    31.             {
    32.                 public String label;
    33.             }
    34.  
    35.             public Rights rights;
    36.  
    37.             public class Rights
    38.             {
    39.                 public String label;
    40.             }
    41.  
    42.             public Summary summary;
    43.  
    44.             public class Summary
    45.             {
    46.                 public String label;
    47.             }
    48.  
    49.             [FormerlySerializedAs("im:price")]
    50.             public Price price;
    51.  
    52.             public class Price
    53.             {
    54.                 public String label;
    55.  
    56.                 public Attributes attributes;
    57.  
    58.                 public class Attributes
    59.                 {
    60.                     public double amount;
    61.                     public String currency;
    62.                 }
    63.             }
    64.  
    65.             [FormerlySerializedAs("im:image")]
    66.             public List<Image> image;
    67.  
    68.             public class Image
    69.             {
    70.                 public String label;
    71.  
    72.                 public Attributes attributes;
    73.  
    74.                 public class Attributes
    75.                 {
    76.                     public int height;
    77.                 }
    78.             }
    79.  
    80.             [FormerlySerializedAs("im:artist")]
    81.             public Artist artist;
    82.  
    83.             public class Artist
    84.             {
    85.                 public String label;
    86.  
    87.                 public class Attributes
    88.                 {
    89.                     public String href;
    90.                 }
    91.             }
    92.  
    93.             public Title title;
    94.  
    95.             public class Title
    96.             {
    97.                 public String label;
    98.             }
    99.  
    100.             public Id id;
    101.  
    102.             public class Id
    103.             {
    104.                 public String label;
    105.  
    106.                 public Attributes attributes;
    107.  
    108.                 public class Attributes
    109.                 {
    110.                     [FormerlySerializedAs("im:id")]
    111.                     public String id;
    112.                 }
    113.             }
    114.  
    115.             [FormerlySerializedAs("im:contentType")]
    116.             public ContentType contentType;
    117.  
    118.             public class ContentType
    119.             {
    120.                 public Attributes attributes;
    121.  
    122.                 public class Attributes
    123.                 {
    124.                     public String term;
    125.                     public String label;
    126.                 }
    127.             }
    128.  
    129.             public Category category;
    130.  
    131.             public class Category
    132.             {
    133.                 public Attributes attributes;
    134.  
    135.                 public class Attributes
    136.                 {
    137.                     [FormerlySerializedAs("im:id")]
    138.                     public String id;
    139.                     public String term;
    140.                     public String scheme;
    141.                     public String label;
    142.                 }
    143.             }
    144.  
    145.             [FormerlySerializedAs("im:releaseDate")]
    146.             public ReleaseDate releaseDate;
    147.  
    148.             public class ReleaseDate
    149.             {
    150.                 public String label;
    151.                 public Attributes attributes;
    152.  
    153.                 public class Attributes
    154.                 {
    155.                     public String label;
    156.                 }
    157.             }
    158.         }
    159.     }
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,146
    http://json2csharp.com/

    Throw your json into there and it will generate the class for you. Then it just depends on what json utility you are using to if you need to add or change the class any.
     
  3. EdgarCarrera

    EdgarCarrera

    Joined:
    Apr 21, 2014
    Posts:
    255
    Thanks for reply but i want to extract on unity. on my Feed var
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,146
    You need the class that can handle the json file. Then you need to use a converter to convert that json into a class. I'm not sure what your question is. That link helps you create your class from the json. Then you get the json string from your source, then you deserialize it into your class and then you access the properties in that class through your instance.

    If your Feed class is correct, you need to use some json converter. Either unity's built in json, or something like jsondotnet on the asset store.
     
  5. rakkarage

    rakkarage

    Joined:
    Feb 3, 2014
    Posts:
    683
  6. EdgarCarrera

    EdgarCarrera

    Joined:
    Apr 21, 2014
    Posts:
    255
    im trying with that but i dont get the result just i got the data

    Code (CSharp):
    1.     IEnumerator Start()
    2.     {
    3.         string url = "https://itunes.apple.com/us/rss/topfreeapplications/limit=20/json";
    4.         WWW www = new WWW(url);
    5.         yield return www;
    6.         if (www.error == null)
    7.         {
    8.             Debug.Log("DATA: " + www.data);
    9.             feed = Database.LoadFromFIle<Feed>(www.text);
    10.         }
    11.         else
    12.         {
    13.             Debug.Log("ERROR: " + www.error);
    14.         }
    15.     }  
    Code (CSharp):
    1. [Serializable]
    2. public class Feed
    3. {
    4.     public Author author;
    5.     [Serializable]
    6.     public class Author
    7.     {
    8.  
    9.         public Name name;
    10.  
    11.         public class Name
    12.         {
    13.             public String label;
    14.         }
    15.  
    16.         public Uri uri;
    17.  
    18.         public class Uri
    19.         {
    20.             public String label;
    21.         }
    22.     }
    23.  
    24.     public List<Entry> entry;
    25.  
    26.     [Serializable]
    27.     public class Entry
    28.     {
    29.         [FormerlySerializedAs("im:name")]
    30.         public Name name;
    31.         [Serializable]
    32.         public class Name
    33.         {
    34.             public String label;
    35.         }
    36.  
    37.         public Rights rights;
    38.         [Serializable]
    39.         public class Rights
    40.         {
    41.             public String label;
    42.         }
    43.  
    44.         public Summary summary;
    45.         [Serializable]
    46.         public class Summary
    47.         {
    48.             public String label;
    49.         }
    50.  
    51.         [FormerlySerializedAs("im:price")]
    52.         public Price price;
    53.         [Serializable]
    54.         public class Price
    55.         {
    56.             public String label;
    57.  
    58.             public Attributes attributes;
    59.             [Serializable]
    60.             public class Attributes
    61.             {
    62.                 public double amount;
    63.                 public String currency;
    64.             }
    65.         }
    66.  
    67.         [FormerlySerializedAs("im:image")]
    68.         public List<Image> image;
    69.         [Serializable]
    70.         public class Image
    71.         {
    72.             public String label;
    73.  
    74.             public Attributes attributes;
    75.             [Serializable]
    76.             public class Attributes
    77.             {
    78.                 public int height;
    79.             }
    80.         }
    81.  
    82.         [FormerlySerializedAs("im:artist")]
    83.         public Artist artist;
    84.         [Serializable]
    85.         public class Artist
    86.         {
    87.             public String label;
    88.             [Serializable]
    89.             public class Attributes
    90.             {
    91.                 public String href;
    92.             }
    93.         }
    94.  
    95.         public Title title;
    96.         [Serializable]
    97.         public class Title
    98.         {
    99.             public String label;
    100.         }
    101.  
    102.         public Id id;
    103.         [Serializable]
    104.         public class Id
    105.         {
    106.             public String label;
    107.  
    108.             public Attributes attributes;
    109.             [Serializable]
    110.             public class Attributes
    111.             {
    112.                 [FormerlySerializedAs("im:id")]
    113.                 public String id;
    114.             }
    115.         }
    116.  
    117.         [FormerlySerializedAs("im:contentType")]
    118.         public ContentType contentType;
    119.         [Serializable]
    120.         public class ContentType
    121.         {
    122.             public Attributes attributes;
    123.             [Serializable]
    124.             public class Attributes
    125.             {
    126.                 public String term;
    127.                 public String label;
    128.             }
    129.         }
    130.  
    131.         public Category category;
    132.         [Serializable]
    133.         public class Category
    134.         {
    135.             public Attributes attributes;
    136.             [Serializable]
    137.             public class Attributes
    138.             {
    139.                 [FormerlySerializedAs("im:id")]
    140.                 public String id;
    141.                 public String term;
    142.                 public String scheme;
    143.                 public String label;
    144.             }
    145.         }
    146.  
    147.         [FormerlySerializedAs("im:releaseDate")]
    148.         public ReleaseDate releaseDate;
    149.         [Serializable]
    150.         public class ReleaseDate
    151.         {
    152.             public String label;
    153.             public Attributes attributes;
    154.             [Serializable]
    155.             public class Attributes
    156.             {
    157.                 public String label;
    158.             }
    159.         }
    160.     }
    161. }
    Code (CSharp):
    1. [System.Serializable]
    2. public class Database : MonoBehaviour
    3. {
    4.     public static T LoadFromFIle<T>(string text)
    5.     {
    6.         T storedData = default(T);
    7.  
    8.         try
    9.         {
    10.             string serializedData = File.ReadAllText(text);
    11.             storedData = JsonUtility.FromJson<T>(serializedData);
    12.  
    13.         }
    14.         catch (System.Exception ex)
    15.         {
    16.             Debug.LogWarning("File reading error: " + ex.Message);
    17.         }
    18.  
    19.         return storedData;
    20.     }
    21. }
     
  7. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,146
  8. EdgarCarrera

    EdgarCarrera

    Joined:
    Apr 21, 2014
    Posts:
    255
  9. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,146
    Why are you doing file.readalltext? You already have the string. Just convert it.
    Just comment out the line and convert the string from the parameter instead.
     
  10. EdgarCarrera

    EdgarCarrera

    Joined:
    Apr 21, 2014
    Posts:
    255
    i search on google and nothing i'm just testing different methods but it's hard
     
  11. EdgarCarrera

    EdgarCarrera

    Joined:
    Apr 21, 2014
    Posts:
    255