Need help parsing Json

Hi
I want to parse some JSon but i’m struggling to understand how it can work. Here is my Json :

{ 
   "full_network":{ 
      "Fixed Nodes":{ 
         "babe":1,
         "hrwx":0,
         "dro0":1,
         "user":1,
         "bd9b":1,
         "b74d":1
      },
      "Network":[ 
         "[bd9b-user-{'bd9b': 1, 'user': 1}]",
         "[b74d-bd9b-{'bd9b': 100, 'b74d': 100}]",
         "[bd9b-dro0-{'dro0': 1, 'bd9b': 1}]",
         "[dro0-user-{'dro0': 1, 'user': 1}]",
         "[b74d-hrwx-{'b74d': 1, 'hrwx': 1}]"
      ],
      "Links":[ 
         [ 
            "bd9b",
            "user",
            1
         ],
         [ 
            "b74d",
            "bd9b",
            100
         ],
         [ 
            "bd9b",
            "dro0",
            1
         ],
         [ 
            "dro0",
            "user",
            1
         ],
         [ 
            "b74d",
            "hrwx",
            1
         ]
      ]
   },
   "command":"get_full_network",
   "error_description":"",
   "request_id":"45"
}

What i’m interested in is the “Links” array. Final purpose is to create a sort of graph (or tree) showing nodes name and links.
I would like to extract those in a list of object like this :

[Serializable]
public class Link
{
    public string peerA;
    public string peerB;
    public int weight;
}

When i receive the json message :

i don’t know the number of links the is in the json message
i don’t know the nodes name
I basically have no idea on how to extract those data :confused:

Here is my lame attempt :

public void handle_fullNetwork()
    {
        string test = "{\"full_network\": {\"Fixed Nodes\": {\"babe\": 1, \"hrwx\": 0, \"dro0\": 1, \"user\": 1, \"bd9b\": 1, \"b74d\": 1}, \"Network\": [\"[bd9b-user-{'bd9b': 1, 'user': 1}]\", \"[b74d-bd9b-{'bd9b': 100, 'b74d': 100}]\", \"[bd9b-dro0-{'dro0': 1, 'bd9b': 1}]\", \"[dro0-user-{'dro0': 1, 'user': 1}]\", \"[b74d-hrwx-{'b74d': 1, 'hrwx': 1}]\"], \"Links\": [[\"bd9b\", \"user\", 1], [\"b74d\", \"bd9b\", 100], [\"bd9b\", \"dro0\", 1], [\"dro0\", \"user\", 1], [\"b74d\", \"hrwx\", 1]]}, \"command\": \"get_full_network\", \"error_description\": \"\", \"request_id\": \"45\"}";
        
        FullNetwork item = JsonUtility.FromJson<FullNetwork>(test);

        Debug.Log(item.Links.Length); // throw an error

    }

[Serializable]
public class Link
{
    public string peerA;
    public string peerB;
    public int weight;
}
[Serializable]
public class FullNetwork
{
    public Link[] Links;
}

If someone is familiar with Json parsing in unity i would love to have some help :smile:

Do you have any control over the incoming JSON? If so, you’ll want to make the links look more like this:

      "Links":[
         {
            "peerA" : "bd9b",
            "peerB" : "user",
            "weight" : 1
         },

If you can’t, then you’ll have to treat each link as an untyped array (that is, object[ ] ) and typecast link[0], link[1], and link[2] into string/string/int respectively when you want to use them, which will get complicated.

I have no control :confused: I will try to play around with untyped object i guess

i made change to :

[Serializable]
public class FullNetwork
{
    public object[] Links;
}

And tryed :

        FullNetwork item = JsonUtility.FromJson<FullNetwork>(test);
        Debug.Log(item.Links);

Item.links is still returning null. What am i doing wrong ?

OK, looking a little deeper into your code now, and there’s several things that need to be taken care of.

First, you need to include the full hierarchy in your class structure, including the exact names. In this case you need an outer class first which is what you give to JsonUtility.FromJson:
(I’m including even the ones you didn’t say you needed just so the data structure is clear, and you never know when you’ll need it)

public class FullJsonFile {
public FullNetwork full_network;
public string command;
public string error_description;
public string request_id;
}

Then FullNetwork needs the objects that that item contains:

public class FullNetwork {
//public Dictionary<string, int> Fixed Nodes; //this won't compile and I don't know if there's a workaround for JSON names that include spaces. This JSON file is just really bad for this application. If you need this at some point you might try FixedNodes or Fixed_Nodes and see if those work. Fortunately if you don't need it we can just comment it out.
public List<string> Network;
public List<List<object>> Links; //decided to use List instead of object[] because the syntax is a little more clear IMO
}

And then we run into a new problem: JsonUtility.FromJson doesn’t support lists or arrays (or dictionaries). It’s dumb and annoying and all that. The good news is that LitJson does support lists, and once LitJson is added to your project, its JsonMapper.ToObject can be directly substituted in for JsonUtility.FromJson with no other syntax changes and it’ll just work, so none of our prior work is wasted.

Now, you should be able to do this:

FullJsonFile jsonObj = JsonMapper.ToObject<FullJsonFile>(test);
foreach(List<object> thisLink in jsonObj.full_network.Links) {
Debug.Log($"peerA is {thisLink[0]}, peerB is {thisLink[1]}, weight is {thisLink[2]}");
}

(code is untested of course so no guarantees it’ll work first try)

1 Like

Wow. And that actually worked on first try ! Thank you so much