1

I have the following code which is looping through a raw_coords JSON.NET JArray object.

List<string> coords_final = new List<string>();
foreach (var item in raw_coords.Children())
{
    var itemcount = item.Count();
    for (int i = 0; i < itemcount; i++) //loop through rows
    {
        var coord_temp1 = item[i].ToString();
        coord_temp1 = coord_temp1.Trim(new Char[] { ' ', ',', '.' });
        coord_temp1 = coord_temp1.Trim(new Char[] { ' ', '[', '.' });
        coord_temp1 = coord_temp1.Trim(new Char[] { ' ', ']', '.' });
        coord_temp1 = coord_temp1.Replace(',', ' ');
        coords_final.Add(coord_temp1);
    }
}

In the above loop, the value of the item variable is something like:

item [ [    -9221176.0728999991,    4120407.4526000023  ],  [    -9221176.0427000001,    4120407.6987999976  ]],[[...],[]]

And, in the data above, the coords_final does become an array with two elements. But what I need is one string variable which will have data in the form of:

(-9221176.0729 4120407.4526000023, -9221176.0427 4120407.6987999976), (..)..

My code above is adapted from another C# program but the data that currently yields is like:

-9221176.0729 4120407.4526000023, -9221176.0427 4120407.6987999976, -9221176.1341 4120407.4602999985,

which is not what I need.

So how can I make the data to be in the desired format--as shown above? I have tried to prepend "(" and append ")" but that didn't work.

8
  • Appending and prepending the parens should work fine. Can you post the code that didn't work and explain what happened? Commented Mar 4, 2015 at 22:19
  • Hmmm. The code deleted--basically, I was ending up with an extra comma at the very end but, as you can see, my code is trying to also remove a comma Commented Mar 4, 2015 at 22:21
  • There might be a better approach to this over at stackoverflow.com/questions/7096474/… . Instead of string building, build a dictionary and then use a serializer on it to get proper json without the string parsing mess. Commented Mar 4, 2015 at 22:25
  • Yes, but I am very close: the 'item' variable is currently populating the coords_final list string. What it needs to do is to input all item variable's data into one element of coords_final with the ( and ) attached. Commented Mar 4, 2015 at 22:28
  • I have edited your title. Please see, "Should questions include “tags” in their titles?", where the consensus is "no, they should not". Commented Mar 4, 2015 at 22:44

2 Answers 2

1

Assuming raw_coords is your Jarray

List<string> myCoordsList = new List<string>();

foreach(JToken item in raw_coords)
{
    List<string> listOfPairs = new List<string>();
    var result = item.ToObject<JArray>();
    foreach (JToken jToken in result)
    {
        var jarray = (JArray) jToken;
        IEnumerable<string> nums = jarray.Values<string>();
        listOfPairs.Add(string.Join(" ", nums));
    }

    myCoordsList.Add(string.Format("({0})", string.Join(",", listOfPairs)));
}
string coordsString = string.Join(",", myCoordsList);

the result is

(-9221176.0729 4120407.4526),(-9221176.0427 4120407.6988),(-9221176.1341 4120407.4603),(-9221176.0729 4120407.4526),(-9221176.104 4120407.7063),(-9221176.1341 4120407.4603) ...

for the first few pairs of coords.

Sign up to request clarification or add additional context in comments.

15 Comments

Did you mean to Deserialize the raw_coords array?
oh... now i see... item contains the rows... i'll update my answer (deserialize item[i]...)
Thanks. Let me try again. BTW, I did Deserialize the loaded JSON in a few lines of code above...not sure if that will have any bearing.
I don't think your code is going to work as it is given: There is already the foreach encompassing your code: foreach (var item in raw_coords.Children())
Yes, working on posting the entire JSON; ...it is big so will give a link
|
0

The following solution gives the result you want:

string json = @"[ 
    [    -9221176.0728999991,    4120407.4526000023  ],  
    [    -9221176.0427000001,    4120407.6987999976  ]
 ]";

JArray raw_coords = JArray.Parse(json);
List<string> coords_final = new List<string>();

foreach (var item in raw_coords.Children())
{
    coords_final.Add("("+String.Join<object>(",", item)+")");
}

string coords = String.Join(",",coords_final.ToArray());

Console.WriteLine (coords);

(-9221176.0729,4120407.4526),(-9221176.0427,4120407.6988)

1 Comment

Thanks! Never got around to trying your solution but sure appreciate your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.