There is a json in which the points are stored, from which, in consequence, certain 3d objects are built, in fact, the coordinates of the vertices. The peculiarity is that they lie at uneven depth.

screenshot of json'a

As can be seen in the 31st "set" of points, they lie immediately after the "coordinates" node and in the 32nd after this node they are further divided into 2 groups, and within the group there is the desired set of points. The task is to go through the nodes to the elements constituting the sets of points (the green elements are the coordinates of xy itself), collect the points belonging to one set in the list, send them for processing and continue moving along json. The difficulty is that where they are divided into groups (32nd), each group of elements must be sent separately, as if it were finished (as in the case of the 31st, we walked 0,1,2 collected points inside sent let's go further), that is, go 32-geometry-> coordinates-> 0 to collect everything inside and send then to 32-geometry-> coordinate-> 1 to send, then just further. It would be logical to assume that there where type: MultiLineString you can simply add another cycle of passing through the elements, but something tells me that I am wrong. How to solve this problem correctly in terms of practicing parsing json files?

Sorry for the messy description, tried to detail as I could.

update in this way I tried to get to the data I needed, but the data from the 32nd group merged into one list and I never figured out how to RIGHT, I get them. Now I understand judging by the comments and answers that I got close to the task from the fundamentally wrong side.

private void CreateRoads(JSONObject mapData) { int count = 0; foreach (var geo in mapData["features"].list) { count++; var l = new List<Vector3>(); for (int i = 0; i < geo["geometry"]["coordinates"].list.Count; i++) { if (!geo["geometry"]["coordinates"][i][0].IsNumber) { for (int j = 0; j < geo["geometry"]["coordinates"][i].list.Count; j++) { var c = geo["geometry"]["coordinates"][i][j]; var bm = GM.LatLonToMeters(c[1].f, c[0].f); var pm = new Vector2(bm.x - Rect.center.x, bm.y - Rect.center.y); l.Add(pm.ToVector3xz()); } } else { var c = geo["geometry"]["coordinates"][i]; var bm = GM.LatLonToMeters(c[1].f, c[0].f); var pm = new Vector2(bm.x - Rect.center.x, bm.y - Rect.center.y); l.Add(pm.ToVector3xz()); } } var m = new GameObject("road_" + count.ToString()).AddComponent<RoadPolygon>(); m.transform.localPosition = new Vector3(m.transform.localPosition.x, m.transform.localPosition.y + 1f, m.transform.localPosition.z); m.transform.parent = this.transform; try { m.Initialize(geo["properties"]["id"].str, this, l, geo["properties"]["kind"].str); } catch (Exception ex) { Debug.Log(ex); } } } 

I managed with deserialization, thanks @ sp7, but I don’t understand how to arrange different levels of depth for the blocks. What would it be universal, and not hardcode. There are thoughts on the topic of the passage to the depth of the last node (it differs in the fact that it has children), from it up to the level, collect the desired data (lists with coordinates), send, go up a level, see if there are any lists, no up again if you went to the coordinates block, then go to the next number (33,34,35) and so on ... the only problem is that I don’t know how to make such a depth passage. And googling did not help much = /

  var root = JsonConvert.DeserializeObject<Rootobject>(www.text); var features = root.roads.features; List<object[][]> coordinates = new List<object[][]>(); for (int i = 0; i < features.Length; i++) { coordinates.Add(features[i].geometry.coordinates); } ConstructRoad(coordinates); 
  • one
    You would put your json in text form for experiments. Well, you do not expect that we reconstruct json manually by the picture? - VladD
  • one
    And yet, it is not clear why you are working with json directly. Why not deserialize first into the object structure? - VladD
  • @VladD added sample code and thoughts about his actions - justyx pm

2 answers 2

Let me speak. As previously noted, in order to be comfortable working with Json data, they must at least be deserialized into an object view.

Therefore:

1) If you have this Json data in text form ( string ), copy it to the clipboard. If there is no data in text form, get it from somewhere outside, convert it to a string (as an option, JObject.Parse().ToString() ) and copy to the clipboard.

2) Create an empty С# file, there should not be anything in it except the indication of the namespace . Put the cursor inside the namespace .

3) Then in Visual Studio go to the menu Edit -> Paste Special -> Paste JSON as Classes .

4) Voila !!! Visual Studio has created classes for you that represent the specified Json data.

5) Now that the С# classes are ready, you can deserialize your Json data using the JsonSerializer into the object model that the studio has created for you.

 var root = JsonConvert.DeserializeObject<Rootobject>(jsonStr); 

6) Well, in the end, now you can simply work with the data in a convenient way.

  • Thank! I'll try to do it now! - justyx 2:17 pm
  • Do I need to copy the entire json at the same time or can I select the necessary block? I mean, if I don’t need all the data, it’s still necessary to describe everything entirely in the classroom, and there’s already a way to get the right thing to do? - justyx 2:19 pm
  • It is better to copy everything so that the structure is formed correctly. Further, when the studio generates the necessary classes, you can always cut out unnecessary members from them. - sp7
  • updated the question description. I do not ask for a ready-made solution, but at least where to dig and what to get acquainted with. - justyx
  • Does it recursively deserialize you? - sp7

Take a look here , how parts of JSON are deserialized ...

General parsing is done here, and then you can go to levels and deserialize them like this:

 JObject googleSearch = JObject.Parse(googleSearchText); // get JSON result objects into a list IList<JToken> results = googleSearch["responseData"]["results"].Children().ToList(); // serialize JSON results into .NET objects IList<SearchResult> searchResults = new List<SearchResult>(); foreach (JToken result in results) { SearchResult searchResult = JsonConvert.DeserializeObject<SearchResult>(result.ToString()); searchResults.Add(searchResult); } 
  • Thank you, I will immediately get acquainted with the information :) - justyx