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
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 ![]()