1

I am trying to search value plus special characters in a json string

my string is String responseBody = {"name":"rohan","age":32,"loca":"NJ"} and to be searched is ActualSearch = "name":"rohan"

res = responseBody.contains(ActualSearch);

I am unable to search this string , IF i give specific values for e.g only "rohan" then it searches fine but "name":"rohan" fails . Please suggest for correct code to search "name":"rohan" as a whole

halfelf
  • 9,737
  • 13
  • 54
  • 63
Rohit
  • 23
  • 5
  • 1
    Suggestion for correct code: Use a JSON parser. Search the parsed data, where white-spaces and escapes are non-issues. – Andreas Sep 09 '19 at 20:33
  • Please provide a code snippet that you have tried so far. – alexandrum Sep 09 '19 at 20:34
  • 1
    I wrote test program for this, you might be able to get the answer by looking at this code: public class CheckColon { public static void main(String[] args) { String responseBody = "{\"name\":\"rohan\",\"age\":32,\"loca\":\"NJ\"}"; System.out.println(responseBody.contains("\"name\":\"rohan\"")); } } – user12142006 Sep 09 '19 at 20:40
  • Your snippet code is actually returning true, so I do not see the issue here. – alexandrum Sep 09 '19 at 20:49

1 Answers1

0

this should work:

String json = "{\"name\":\"rohan\",\"age\":32,\"loca\":\"NJ\"} ";
        String actualSearch = "\"name\":\"rohan\"";

        System.out.println(json.contains(actualSearch));

You should looking JSON Parsers for issues with escape characters

Syrup72
  • 116
  • 1
  • 12
  • thanks this worked but is there any other way to just search "name":"rohan" in response string json, this will help in printing the searched values . I am already doing below vjson1 = json.replaceAll("\\\\", ""); to delete the "\" from actual json . Then I get json without "\" and then I need to search "name":"rohan" in vjson1. So can we search without inserting "\" in actualSearch . – Rohit Sep 09 '19 at 22:12
  • If the above answered your question, please mark it :). as for what you are asking, the representation of a jsonstring is with "\", its used to escape the "". Please read this https://stackoverflow.com/questions/2591098/how-to-parse-json-in-java to get an idea on the Json Parsers, it will come in handy in the future. – Syrup72 Sep 09 '19 at 23:25
  • sorry this worked for single one but when I am trying in my actual test it is not working , my string is json = ["{\"isEnriched\":\"true\",\"event\":{\"commonEventHeader\":{\"startEpochMicrosec\":\"1553801673000000\",\"sourceId\":\"\",\"eventId\":\"10.190.201.2-FanFail\",\"internalHeaderFields\":{\"resourceInstanceId\":\"\",\"firstDatetime\":\"Sun, 06 Jan 51208 15:50:00 +0000\",\"serviceInstanceId\":\"N/A\"}}}"] and I am trying to search "\"eventId\":\"0.190.201.2-FanFail\"" . This failed,please suggest what should be my data to srch – Rohit Sep 10 '19 at 15:51
  • the eventId in your Json is : "10.190.201.2-FanFail" and your search string is "0.190.201.2-FanFail" – Syrup72 Sep 10 '19 at 16:46