0
\$\begingroup\$

I am working on converting and displaying the JSON String with unity. I have done coding for it

using UnityEngine;
using System.Collections;
using LitJson;
using SimpleJSON;
using System.IO;
public class TestDelete : MonoBehaviour {


JsonData json;
void Start()
{
    Data2 data = new Data2();
    Payload2 data2 = new Payload2();
    data.command = "state";
    data.payload = new Payload2()
    {
        text = "wwwwwww", 
        image = "hello" 

    };
    data2.option = new Option () {
        A1 = "k",
        A2 = "J"

    };

    string dataValue =data+ data2.ToString();
    string json = JsonUtility.ToJson(data,true);
    P(json + "\t\n");

}

// Use this for initialization
void P(string aText)
{
    print (aText +"\n");
}

}

  [System.Serializable]
  public class Payload2
   {
     public string text;
     public string image;
      public Option option;


    }

  [System.Serializable]
   public class Data2
   {
     public string command;
     public Payload2 payload;
   }

   [System.Serializable]
   public class Option
    {
      public string A1;
      public string A2;
    }

This code of working fine.But with the output Iam having problem.The out is displayed as follow

{
"command": "state",
"payload": {
    "text": "wwwwwww",
    "image": "hello",
    "option": {
        "A1": "", // not getting displayed
        "A2": ""   // not getting displayed
    }
}

}

the string value A1 and A2 is not getting displayed.What will be the issue with my code.Can anybody help me sorting out this issues

\$\endgroup\$
2
\$\begingroup\$

The strings are on initial value because you never set data.payload.option to anything.

You set up a variable Payload2 data2, but you never assign it in any way to anything of the variable data.

Try replacing data2.option = new Option () { with data.payload.option = new Option () {

By the way: I have no idea what the line string dataValue =data+ data2.ToString(); is supposed to do, because the variable dataValue isn't used anywhere.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Will I be able to display only some part of the json string.for example : under "option" can I able to display only A1. \$\endgroup\$ – user1509674 Sep 27 '16 at 11:55
  • \$\begingroup\$ @user1509674 This appears to be a new question. \$\endgroup\$ – Philipp Sep 27 '16 at 11:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.