0

im tring to assign value from json to java String. but JSON value is including some special charactor ("\"). when i was try to assigen it to the string it gives error.

this is the JSON value,

"ValueDate":"\/Date(1440959400000+0530)\/"

this is how i trying to use it.

HistoryVO.setValueDate(DataUtil.getDateForUnixDate(historyJson.getString("ValueDate")));

or

enter image description here

MadukaJ
  • 722
  • 6
  • 22
  • 1
    So you want tog get rid of the special character?? just Date(1440959400000+0530) – Rod_Algonquin Jul 30 '14 at 03:27
  • What format of String does `DataUtil.getDateForUnixDate(String)` expect? – Elliott Frisch Jul 30 '14 at 03:31
  • yes Rod_Algonquin i want to get Date(1440959400000+0530) only. @Elliott i want just only to get Date(1440959400000+0530) part, because in my programme there are several type Date format we are using. – MadukaJ Jul 30 '14 at 03:43

4 Answers4

2

Given that

I want ... to get [the] Date(1440959400000+0530) part,

I would use

String value = "/Date(1440959400000+0530)/";
int pos1 = value.indexOf("Date(");
if (pos1 > -1) {
  int pos2 = value.indexOf(")", pos1);
  if (pos2 > pos1) {
    value = value.substring(pos1, pos2 + 1);
    System.out.println(value);
  }
}

Output is

Date(1440959400000+0530)

Note: This works by looking for "Date(" and then the next ")", and it removes everything not between those two patterns.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • the problem is it can not make string "\/Date(1440959400000+0530)\/" like this. because of "\". JSON is comming like this "ValueDate":"\/Date(1440959400000+0530)\/" – MadukaJ Jul 30 '14 at 03:55
  • @MacDaddy Those must be Strings, and not compile time literals. It's fine with the code above because I ignore *everything* to the left of "Date(" and then everything to the right of ")". – Elliott Frisch Jul 30 '14 at 03:56
1

Mac,

As you asked for something like

String ValueDate = "\/Date(1440959400000+0530)\/";

The above one is not possible in java string, As it shows as invalid escape sequence, So replace the slash '\' as double slash '\' as below,

String ValueDate = "\\/Date(1440959400000+0530)\\/";

If am not clear of our question, pls describe it clearly

Regards, Hari

Harry
  • 3,072
  • 6
  • 43
  • 100
  • yes Hari json string is comming like "\/Date(1440959400000+0530)\/" but i couldnt anable to parse it as a Java String. so before assign it to the string i want to format or split it. Regards – MadukaJ Jul 30 '14 at 03:46
1

If you have specific character, ( and ), use substring method to get the value.

    String value = "\\/Date(1440959400000+0530)\\/";
    int start = value.indexOf("(");
    int last = value.lastIndexOf("0");
    value = value.substring(start + 1, last + 1);
    System.out.println(value); <--- 1440959400000+0530

    DataUtil.getDateForUnixDate(value);

I don't know DataUtil.getDateForUnixDate() method, but take care of + character because of it is not number string.

Update

To remove / character use replace method.

    String value = "/Date(1440959400000+0530)/";
    value = value.replace("/", "");
    System.out.println(value);

output

Date(1440959400000+0530)
Zaw Than oo
  • 9,651
  • 13
  • 83
  • 131
  • No CycDemo the thing is value = "\/Date(1440959400000+0530)\/" not value = "\\/Date(1440959400000+0530)\\/". thats the problem. there are no double \ in the json value. – MadukaJ Jul 30 '14 at 03:52
  • My target to say is, if you have `(` and `)`, use `substring` method – Zaw Than oo Jul 30 '14 at 03:54
0

i found the answer for my own question.

historyJson.getString("ValueDate");

this return the String like /Date(1440959400000+0530)/

now i can split it. thank you all for the help.

regards, macdaddy

MadukaJ
  • 722
  • 6
  • 22