1

I've got a big string file (originally geojson) that I need to rectify before using it in my android project.

I explain : i've converted a shapefile into a geojson file but the converter did something wrong. He puts a double array for the coordinates, and android is unnable to parse it.

{
        "type": "Feature",
        "properties": {
            "id": 00001,
            "poi": "cinemas",
            "other": "null"
        },
        "geometry": {
            "type": "MultiPoint",
                    "coordinates": [
                  [ // here is the unwanted character #1
                    7.0000000000000,
                    48.0000000000000
                  ] // here is the unwanted character #2
            ]
        }
}

How to generate a proper string that remove line 11 & 14 for each Json object in this string ?

I tried this, but wasn't working :

string[] x = myJsonString.Split('\n');
x.Remove(x.LastIndexOf(Environment.NewLine)-4);
x.Remove(x.LastIndexOf(Environment.NewLine)-7);

Am I in the wrong way ? Or StringBuilder can do it ? Thanks in advance !

Nawako
  • 342
  • 3
  • 17
  • The double array is perfectly valid in json. The id value of 00001 without quotes around it is what makes this invalid json. If there were quotes around 00001 you could parse this in any json parser... – Chris Jun 02 '15 at 15:23
  • @Chris the id can be a number, and this isn't a problem. See `coordinates`, which are also numbers without quotes. If he's using a strict mode on his json parser, the json string will have to match his object model exactly which is probably why this is failing.. – edthethird Jun 02 '15 at 15:27
  • ahhh. thank you @edthethird. The leading zeros are what i put into jsonlint and caught an error for them – Chris Jun 02 '15 at 15:36
  • @Chris good point, it will be treated as a numerical value of `1`. If the leading 0s are important, then yeah, it should be a string. – edthethird Jun 02 '15 at 16:18
  • Like @edthebird said, the json parser doesn't support double array in this case. Runtime exception when launched. – Nawako Jun 02 '15 at 19:12
  • yeah, so `coordinates` is defined as a `double[]` in your code, it should be a `double[][]` – edthethird Jun 02 '15 at 20:09
  • 1
    Are you sure about the java tag? This looks like C# to me. – stuXnet Jun 02 '15 at 20:12
  • @edthethird got another error when I define the variable as a double[][] : Newtonsoft.Json.JsonSerializationException: Error converting value 7,000000000000000 to type 'System.Single[]'. Path '[0].geometry.coordinates[0]', line 14, position 38. ---> System.ArgumentException: Could not cast or convert from System.Double to System.Single[]. – Nawako Jun 03 '15 at 08:41
  • @stuxNet, yes sorry wrong tag, it was C# in my code (i'm using xamarin with android) – Nawako Jun 03 '15 at 08:41
  • @Nawako can you post the class you are de-serializing into? If you do I can post an answer. – edthethird Jun 03 '15 at 15:12
  • @edthebird sure, [here it is !](http://www.casimages.com/i/150603100310213980.png.html), good luck, because it's seems harder than we believe :) – Nawako Jun 03 '15 at 20:03

3 Answers3

1

You can use Regular Expression and its grouping feature.

// Define your RegEx
Pattern p = Pattern.compile("\\[.*(\\[.*\\]).*\\]");

// Apply this RegEx on your raw string
Matcher m = p.matcher(your_raw_string);

// A container for output string
StringBuffer s = new StringBuffer();

// Iterate over each occurrence of the substring 
while (m.find()) {

    // Append to the output and replace each occurrence with group #1 
    m.appendReplacement(s, m.group(0));
}

// Your desired text!
System.out.println(s.toString());

Reference
More information about using RegEx in Java

Community
  • 1
  • 1
frogatto
  • 28,539
  • 11
  • 83
  • 129
  • I don't why the application crash, [here is the picture.](http://www.casimages.com/i/150603113111848037.png.html) Don't know neither why it needs an array here.. – Nawako Jun 03 '15 at 09:32
  • @Nawako You've gotten an `ArrayIndexOutOfBoundsException`. Please tell me which line generates this exception? – frogatto Jun 03 '15 at 09:58
  • `m.appendReplacement(s, m.group(1));` generate this exception. for more details : `[MonoDroid] java.lang.ArrayIndexOutOfBoundsException: length=2; index=2 [MonoDroid] at java.util.regex.Matcher.group(Matcher.java:358)` – Nawako Jun 03 '15 at 10:55
  • @Nawako You should use `m.group(0)`. I was wrong! I'm sorry. – frogatto Jun 03 '15 at 12:16
  • @Nawako Also you should escape that square brackets. – frogatto Jun 03 '15 at 12:19
  • we're close to the solution. No more error but it is my first time using regex so I don't know where am I (joke). I watched the links (incl. more information about using RegEx) but it's pretty hard to understand. For me it was as simple as Pattern.compile("[.*(\[.*\]).*]") but lead to an error : "**unrecognized escape sequence \ [**" so i've put your new edited code with (double escape) `("\\[.*(\\[.*\\]).*\\]")` now it's working but string is empty – Nawako Jun 03 '15 at 12:56
  • @Nawako I'm not enough familiar with RegEx. All I wanted was getting you started with Regular Expressions. You can play with these symbols to getting to your desired situation. – frogatto Jun 03 '15 at 14:41
0

try:

myString = Regex.Replace(myString, "[( )*[", "[");
myOtherString = Regex.Replace(myOtherString, "]( )*]", "]");
GregH
  • 5,125
  • 8
  • 55
  • 109
  • something is wrong with this expression: result output = the same as input. [Here is the picture related](http://www.casimages.com/i/150603112434264764.png.html) – Nawako Jun 03 '15 at 09:24
  • hmm i tested with a string = two brackets seperated by 20 whitespaces and it worked. must be the new line . let me take a look – GregH Jun 03 '15 at 13:07
  • tried with `gson = gson.Replace ("]( \n )*]", "]");` but same result. thanks for your help – Nawako Jun 03 '15 at 13:16
0

Use a regex replaceAll:

myJsonString = myJsonString.replaceAll("([\\[\\]])[\\s\\n]+?(?=\\1)", "");

Demo here.

James Buck
  • 1,640
  • 9
  • 13
  • It sounds good, but doesn't work when a second json object is entered. For example, if we duplicate the json present in the demo, the result become wrong. – Nawako Jun 03 '15 at 08:53
  • @Nawako You're right, I fixed it now. Make sure you remove the comments you added to those lines first though. – James Buck Jun 03 '15 at 14:40