简体   繁体   中英

How to extract values from a String that cannot be converted to Json

While processing the DialogFlow Response object, I get the below given string as textPayload . If this is a Json string, I can easily convert it to a JSONObject and then extract the values. However, could not convert this to a Json Object. How do I get the values for the keys in this string? What is a good way to parse this string in Java ?

String to be processed

Dialogflow Response : id: "XXXXXXXXXXXX"
lang: "en"
session_id: "XXXXX"
timestamp: "2020-04-26T16:38:26.162Z"
result {
  source: "agent"
  resolved_query: "Yes"
  score: 1.0
  parameters {
  }
  contexts {
    name: "enaccaccountblocked-followup"
    lifespan: 1
    parameters {
    }
  }
  metadata {
    intent_id: "XXXXXXXXXXXX"
    intent_name: "EN : ACC : Freezing Process - Yes"
    end_conversation: true
    webhook_used: "false"
    webhook_for_slot_filling_used: "false"
    is_fallback_intent: "false"
  }
  fulfillment {
    speech: "Since you have been permanently blocked, please request to  unblock your account"
    messages {
      lang: "en"
      type {
        number_value: 0.0
      }
      speech {
        string_value: "Since you have been permanently blocked, please request to  unblock your account."
      }
    }
  }
}
status {
  code: 200
  error_type: "success"
}

If I understand you correctly the issue here is that the keys do not have quotations marks, hence, a JSON parser will reject this.

Since the keys all start on a new line with some white-space and all end with a colon: you can fix this easily with a regular expression.

See How to Fix JSON Key Values without double-quotes?

You can then parse it to a Map via

Map<String, Object> map 
  = objectMapper.readValue(json, new TypeReference<Map<String,Object>>(){});

(but I assume you are aware of this).

Convert it to valid json, then map using one of the many libraries out there.

You'll only need to:

  • replace "Dialogflow Response:" with {
  • add } to the end
  • add commas between attributes, ie
    • at the end of every line with a ":"
    • after "}" , except when the next non-whitespace is also "}"

Jackson (at least) can be configured to allow quotes around attribute names as optional.

Deserializing to a Map<String, Object> works for all valid json (except an array, which this isn't).

Create a class for TextPayload object like this.

public class TextPayload {
   private int session_id;
   private String lang;
   private String timestamp;
   private String[] metadata ;
   //Other attributes
   //getters setters
}

Then using an ObjectMapper extract the values from textpayload like this:

    ObjectMapper mapper = new ObjectMapper();
    TextPayload textPayload = mapper.readValue(output, User.class);

To utilize ObjectMapper and hands on with it followthis

you can use the nodejs package parse-dialogflow-log to parse the textResponse string.

  1. replace "Dialogflow Response:" with "{"
  2. add "}" to the end
  3. run the package on the result and you'll get a nice json.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM