com.novemser.voicetest.utils.JsonParser.java Source code

Java tutorial

Introduction

Here is the source code for com.novemser.voicetest.utils.JsonParser.java

Source

/*******************************************************************************
 * Copyright (c) <2016> <Novemser>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
 *  files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify,
 *  merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
 *  furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
 * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 *  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 *  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 *  SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 ******************************************************************************/

package com.novemser.voicetest.utils;

/**
 * Created by Novemser on 5/24/2016.
 */
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import android.util.Log;

import java.sql.SQLClientInfoException;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * Json?
 */
public class JsonParser {

    public static ArrayList<NewsResult> parseNewsInfo(String json) {
        ArrayList<NewsResult> results = new ArrayList<>();
        try {
            JSONTokener tokener = new JSONTokener(json);
            JSONArray array = (JSONArray) tokener.nextValue();
            int len = array.length();
            for (int i = 0; i < len; i++) {
                JSONObject object = array.getJSONObject(i);

                results.add(new NewsResult(object.getString("url"), object.getString("title"),
                        object.getString("time")));
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return results;
    }

    public static String parseIatResult(String json) {
        StringBuffer ret = new StringBuffer();
        try {
            JSONTokener tokener = new JSONTokener(json);
            JSONObject joResult = new JSONObject(tokener);

            JSONArray words = joResult.getJSONArray("ws");
            for (int i = 0; i < words.length(); i++) {
                // ?
                JSONArray items = words.getJSONObject(i).getJSONArray("cw");
                JSONObject obj = items.getJSONObject(0);
                ret.append(obj.getString("w"));
                //            ??
                //            for(int j = 0; j < items.length(); j++)
                //            {
                //               JSONObject obj = items.getJSONObject(j);
                //               ret.append(obj.getString("w"));
                //            }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ret.toString();
    }

    public static String parseGrammarResult(String json) {
        StringBuffer ret = new StringBuffer();
        try {
            JSONTokener tokener = new JSONTokener(json);
            JSONObject joResult = new JSONObject(tokener);

            JSONArray words = joResult.getJSONArray("ws");
            for (int i = 0; i < words.length(); i++) {
                JSONArray items = words.getJSONObject(i).getJSONArray("cw");
                for (int j = 0; j < items.length(); j++) {
                    JSONObject obj = items.getJSONObject(j);
                    if (obj.getString("w").contains("nomatch")) {
                        ret.append("?.");
                        return ret.toString();
                    }
                    ret.append("?" + obj.getString("w"));
                    ret.append("?" + obj.getInt("sc"));
                    ret.append("\n");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            ret.append("?.");
        }
        return ret.toString();
    }

    public static String parseLocalGrammarResult(String json) {
        StringBuffer ret = new StringBuffer();
        try {
            JSONTokener tokener = new JSONTokener(json);
            JSONObject joResult = new JSONObject(tokener);

            JSONArray words = joResult.getJSONArray("ws");
            for (int i = 0; i < words.length(); i++) {
                JSONArray items = words.getJSONObject(i).getJSONArray("cw");
                for (int j = 0; j < items.length(); j++) {
                    JSONObject obj = items.getJSONObject(j);
                    if (obj.getString("w").contains("nomatch")) {
                        ret.append("?.");
                        return ret.toString();
                    }
                    ret.append("?" + obj.getString("w"));
                    ret.append("\n");
                }
            }
            ret.append("?" + joResult.optInt("sc"));

        } catch (Exception e) {
            e.printStackTrace();
            ret.append("?.");
        }
        return ret.toString();
    }

    public static HashMap parseSemanticResult(String json) {
        HashMap<String, String> map = new HashMap<>();
        try {
            JSONTokener tokener = new JSONTokener(json);
            JSONObject joResult = new JSONObject(tokener);

            if (json == null || json.equals("") || !joResult.has("semantic")) {
                map.put("semanticNull", "true");
                return map;
            }

            if (joResult.has("operation"))
                map.put("operation", joResult.getString("operation"));

            if (joResult.has("message"))
                map.put("message", joResult.getString("service"));

            JSONObject semantic = joResult.getJSONObject("semantic");
            if (semantic.has("slots")) {
                JSONObject slots = semantic.getJSONObject("slots");

                if (slots.has("code"))
                    map.put("code", slots.getString("code"));

                if (slots.has("name"))
                    map.put("name", slots.getString("name"));

                if (slots.has("content"))
                    map.put("content", slots.getString("content"));

                if (slots.has("datetime")) {
                    JSONObject dateTime = slots.getJSONObject("datetime");

                    if (dateTime.has("dateOrig"))
                        map.put("dateOrig", dateTime.getString("dateOrig"));

                    if (dateTime.has("time"))
                        map.put("time", dateTime.getString("time"));

                    if (dateTime.has("timeOrig"))
                        map.put("timeOrig", dateTime.getString("timeOrig"));

                    if (dateTime.has("date"))
                        map.put("date", dateTime.getString("date"));
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return map;
    }
}