Unity myjson

Hello,

I would like to make some form of newsfeed in my game using http://myjson.com/

If I save my json it will provide me with a URI to access my json.

URI JSON SAMPLE:
https://api.myjson.com/bins/1fyny9

MYJSON API:
http://myjson.com/api

For the json parser and builder Im using http://wiki.unity3d.com/index.php/SimpleJSON

I have the following:

    IEnumerator RequestNews(WWW www)
    {
        yield return www;
        if(www.error == null)
        {
            var N = JSON.Parse(www.text);
            string value = N["sample"]["Title"];
            Debug.Log(N);
        }
        else
        {
            Debug.Log(www.error);
            gameManager.newsTexts [0].text = "Newsfeed";
            gameManager.newsTexts [1].text = "No news for now.
Please check back later!".Replace("
","\n");
        }
    }


The api of the myjson is a bit confusing for me.
In my URI JSON SAMPLE I would like to access the value of the “Title” and the “Description”.

Can anyone help me to accomplish this?

Thank you!

I don’t understand what it is you think you haven’t already accomplished. A very, very minor tweak of your code populates a field with the title. You can see the result in the inspector.

using SimpleJSON;
using System.Collections;
using UnityEngine;

public class GetNews : MonoBehaviour {
  public string Title;

  // Use this for initialization
  void Start () {
    StartCoroutine(RequestNews(new WWW("https://api.myjson.com/bins/1fyny9")));
  }

  // Update is called once per frame
  void Update () {
    }

  IEnumerator RequestNews(WWW www)
  {
    yield return www;
    if (www.error == null)
    {
      var N = JSON.Parse(www.text);
      Title = N["sample"]["Title"];
      Debug.Log(Title);
    }
    else
    {
      Debug.Log(www.error);
    }
  }
}

Thank you so much!

Now I can finally change the text of the json and it will update on start! EXCELLENT!!!

I don’t understand what’s happening, here… I just told you that your code was basically already working. Anyway. You’re welcome?