无法从包含冒号的Json字符串生成键值对

时间:2018-07-17 21:22:45

标签: java json

无法将JSON数据解析为键值对。

当我删除冒号示例:“ 28.08.04.92”时,它可以工作。

错误:不是JSON对象:“ 28:08.04.92”

预期结果:

AHFS:["28:08.04.92"]:0
Action:"":0
AdminInstructions:"Take 2 capsules by mouth daily":0
MedicationOrders:[{"AHFS":["28.08.04.92"],"Action":"","AdminInstructions":"Take 2 capsules by mouth daily"}]:0

代码:

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import com.google.gson.*;
import com.google.gson.JsonObject;
import com.google.gson.JsonElement;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;
import org.json.*;
public class JsonParsing {


static JsonParser parser = new JsonParser();


public static void createHashMapFromJsonString(String json, int row_num) 

    {

        JsonObject object = (JsonObject) parser.parse(json.toString());
        Set<Map.Entry<String, JsonElement>> set = ((JsonObject) object).entrySet();
        Iterator<Map.Entry<String, JsonElement>> iterator = set.iterator();
        ArrayList<String> map = new ArrayList<String>();
        while (iterator.hasNext()) {

            Map.Entry<String, JsonElement> entry = iterator.next();
            String key = entry.getKey();
            JsonElement value = entry.getValue();

            if (null != value) {
                if (!value.isJsonPrimitive()) {
                    if (value.isJsonObject()) {

                        createHashMapFromJsonString(value.toString(), row_num);

                    } else if (value.isJsonArray() && value.toString().contains(":")) {

                        ArrayList<String> list = new ArrayList<>();
                        JsonArray array = value.getAsJsonArray();
                        if (null != array) {
                            for (JsonElement element : array) {
                                createHashMapFromJsonString(element.toString(), row_num);
                            }
                            map.add(key+list);
                        }
                    } 
                    else if (value.isJsonArray() && !value.toString().contains(":")) 
                    {
                        map.add(key+value.getAsJsonArray());
                    }
                } 
                else 
                {
                    map.add(key+value.getAsString());
                }
            }
            map.add(key+value.toString());
            System.out.println(key+":"+value+":"+row_num);
        }


}

public static void main(String args[]) 
{
    String json_input = "{\"MedicationOrders\":[{\"AHFS\":[\"28:08.08\"]}]}";
    int row_num = 0;
    if(json_input.trim().charAt(0) == '[')
    {
        try 
        {
        JSONArray jsonArray = new JSONArray(json_input);
        for (int i = 0; i < jsonArray.length(); i++)
        {       
        JSONObject jsonobject = jsonArray.getJSONObject(i);
        String json = jsonobject.toString();
        createHashMapFromJsonString(json,row_num);  
        row_num++;
        }
        }
        catch (JSONException e)
         {
        }
    } 
    else 
    {

        createHashMapFromJsonString(json_input,row_num);
    }

}   
}

1 个答案:

答案 0 :(得分:0)

发生此问题是因为您要将JsonPrimitive强制转换为JsonObject,这是我在运行代码时遇到的错误:

  com.google.gson.JsonPrimitive cannot be cast to com.google.gson.JsonObject
    at JsonParsing.createHashMapFromJsonString(JsonParsing.java:21)
    at JsonParsing.createHashMapFromJsonString(JsonParsing.java:43)
    at JsonParsing.createHashMapFromJsonString(JsonParsing.java:43)
    at JsonParsing.main(JsonParsing.java:89)

您必须选择更改json字符串并将数组删除为以下"{\"MedicationOrders\":[{\"AHFS\":\"28:08.08\"}]}"
或修改代码以解析JsonPrimitive数组时的array值,如下所示:

 .
 .
 .
else if (value.isJsonArray() && value.toString().contains(":")) {

                    ArrayList<String> list = new ArrayList<>();
                    JsonArray array = value.getAsJsonArray();
                    if (null != array) {
                        for (JsonElement element : array) {
                            if (!element.isJsonPrimitive()) {
                                createHashMapFromJsonString(element.toString(), row_num);
                            }else{
                                list.add(element.toString());
                            }
                        }
                        map.add(key + list);
                    }
                }