How to display data from json with SimpleJSON?

crepy1234 picture crepy1234 · Sep 16, 2013 · Viewed 13.6k times · Source

I want to show multiple values from JSON, but cannot. I have tried to find a solution to my problem, but have been unsuccessful. Therefore, I ask question and hope that somebody will help me.

This is my JSON data:

{
 projects: [
  {
   id: "1",
   name: "sssssdd"
  },
  {
   id: "2",
   name: "ccccc"
  },
  {
   id: "3",
   name: "dasdasd"
  }
 ]
}

And this my code:

using UnityEngine;
using System.Collections;
using SimpleJSON;

public class simpleRequest : MonoBehaviour {

    IEnumerator SendRequest()
    {
        WWW request = new WWW("http://localhost:9999/post/results.json");


        yield return request;

        if (request.error == null || request.error == "")
        {

            var N = JSON.Parse(request.text);

            Debug.Log(N["projects"][0]["name"]);

        }
        else
        {
            Debug.Log("WWW error: " + request.error);
        }
    }

    void Start()
    {
        StartCoroutine(SendRequest());
    }
}

Please excuse me! I'm not good at English.

Answer

mattsson picture mattsson · Sep 17, 2013

You should use Debug.Log(N["projects"][0]["name"].Value); or N["projects"][0]["name"].AsInt, N["projects"][0]["name"].AsFloat, etc. for other data types.