0
 IEnumerator GetJsonvalues()
{

    string URLs = "http://www.json-generator.com/api/json/get/cfmxqecgky?indent=2";

    WWW readjson = new WWW(URLs);
    yield return readjson;



    if(string.IsNullOrEmpty(readjson.error))
    {
        JSONDataString = readjson.text;


        Jsondatasaver obj = JsonUtility.FromJson<Jsondatasaver>(JSONDataString);

        Debug.Log("Total Pages " +obj.getdata[0]);


    }




}

The above URL contains JSON data.I have done a similar one without any problem.For practise I just changed the URL and created a class with the names to call the JSON values using a POJO class.I have created a list to call the values inside a JSON array.So while calling obj.getdata[0].ANYVALUES I am getting arugument exception error.Cant undertsand why?

  using System.Collections;
using System.Collections.Generic;

[System.Serializable]

public class Insidevalues
{

public string color;
public string pantone_value;
public int year;
public int id;
public string name;


public Insidevalues(string Color, string Pantone, int Year, int Id, string Name)
{
    this.color = Color;
    this.pantone_value = Pantone;
    this.year = Year;
    this.id = Id;
    this.name = Name;

}

}

[System.Serializable]
    public class Jsondatasaver 

{
    public int per_page;
    public int total;
    public int total_pages;
    public int page;
    public List<Insidevalues> getdata; 

}
DaveRead
  • 3,371
  • 1
  • 21
  • 24
zyonneo
  • 1,319
  • 5
  • 25
  • 63
  • 2
    Property names you provide and the names from JSON are different – Hasan Emrah Süngü Dec 21 '18 at 09:51
  • 3
    Looking at that URL, the JSON has a "data" property but no "getdata". Did you perhaps mean to either all your field "data" or add an attribute to say which JSON property to expect? (As an aside, I'd strongly urge you to avoid public fields, and follow .NET naming conventions - use ``JsonProperty` to indicate how you expect the JSON properties to be named.) – Jon Skeet Dec 21 '18 at 09:51
  • @Jon Skeet ....Need to study.... .NET naming conventions .. Thanks mates... – zyonneo Dec 21 '18 at 09:53
  • If you rename the property getdata on Jsondatasaver to data then the JSON should serialize. The index out of range error is being generated because the List has not been serialized. – DaveRead Dec 21 '18 at 10:14
  • @Jon Skeet what happens if the property is a number...like this http://www.json-generator.com/api/json/get/bOQGnptixu?indent=2 – zyonneo Dec 22 '18 at 05:11
  • In the JSON value "data" contains many properties...My problem is if instead of "data".......if any numerical value comes like "1" or "2"...how will I initialise my List?public List "1";(this format is wrong) – zyonneo Dec 22 '18 at 05:41
  • @zyonneo: Then you should use a dictionary instead. There are lots of Stack Overflow questions about this. If that's not enough to help you research it, ask a new question. – Jon Skeet Dec 22 '18 at 12:42
  • @JonSkeet tried but cant find any correct questions..if I ask they say it is duplicate – zyonneo Dec 26 '18 at 13:22
  • Then follow the links to the questions closed as duplicates. Even if they're not *exactly* the same, they're likely to be close enough to help you with a bit of thought. – Jon Skeet Dec 26 '18 at 14:30

0 Answers0