Extract data from a json file using Simplejson?

Asked

Viewed 1,323 times

1

Json file 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 am using Windows 7 operating system 64 bit version.

I am trying to extract data from the above json file 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 that ? Thank you.

1 answer

1

You’re probably not succeeding because you imported Simplejson but not Streamreader, which is what you actually use.

For you to do what you want, you’ll need to deserialize the file .json see how your code can look:

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 in this link: https://stackoverflow.com/questions/18538428/loading-a-json-file-into-c-sharp-program

Browser other questions tagged

You are not signed in. Login or sign up in order to post.