Extract data from a json file using SimpleJson?

1

json file that I'm using.

{ 
"url" : "https://scontent-gru2-1.cdninstagram.com/hphotos-xaf1/t51.2885-     15/s320x320/e15/12523558_1184627644899567_723648549_n.jpg", 
"low_resolution" : "https://scontent-gru2-1.cdninstagram.com/hphotos-xfp1/t51.2885-15/s320x320/e15/1168874_1275902905759358_1285783491_n.jpg", 
"thumbnail": "https://scontent-gru2-1.cdninstagram.com/hphotos-xfp1/t51.2885-15/s320x320/e35/1169888_444355602430404_585844977_n.jpg", 
"standart" : "https://scontent-gru2-1.cdninstagram.com/hphotos-xaf1/t51.2885-15/s320x320/e35/12424518_166030477094416_1436870704_n.jpg" 
 }

I'm using the Windows 7 64-bit operating system.

I'm trying to extract data from the json file above and get a string in C # language. But when I run this code in Unity nothing appears on the console.

using SimpleJSON;
using System;

public class ReadJson : MonoBehaviour {
    static void Main(string[]args){

        StreamReader input = new StreamReader("Assets/Resources/test.json");
        System.Console.WriteLine(input.ReadToEnd());
    }
}

Would anyone have an idea of an easier way to do this? Thank you.

    
asked by anonymous 12.01.2016 / 21:29

1 answer

1

You're probably not getting why you imported SimpleJSON but not StreamReader, which is what you actually use.

To do what you want, you'll need to deserialize the file .json see how your code can stay:

using System;
using System.Collections.Generic;
using Newtonsoft.Json;

public class RootObject
{
    public string url { get; set; }
    public string low_resolution { get; set; }
    public string thumbnail { get; set; }
    public string standart { get; set; }
}

public class ReadJson : MonoBehaviour
{
    static public void Main()
    {
       using (StreamReader r = new StreamReader(Server.MapPath("~/test.json")))
       {
           string json = r.ReadToEnd();
           List<RootObject> ro = JsonConvert.DeserializeObject<List<RootObject>>(json);
       }

       Console.WriteLine(ro[0].url_short);
    }  
}

There are also similar answers to your question at this link: link

    
08.09.2016 / 19:23