简体   繁体   English

在java中将逗号分隔的字符串转换为json

[英]convert a comma separated string to json in java

I have a comma separated string which needs to converted into a JSON string, When I make the JSONArray conversion it is still in array format.我有一个逗号分隔的字符串,需要转换为 JSON 字符串,当我进行 JSONArray 转换时,它仍然是数组格式。 What is the best way to solve this.解决这个问题的最佳方法是什么。

String str="art,0.0, comedy,0.0, action,0.0, crime,0.0, animals,0.0"

expected output预期产出

{"art":"0.0", "comedy":"0.0","action":"0.0","crime":"0.0","animals":"0.0"}

Here is the code I tried这是我试过的代码

String [] arrayStr=strVal.split(",");
JSONArray mJSONArray = new JSONArray();
for (String s: arrayStr){
    mJSONArray.put(s);
}
System.out.println(mJSONArray.toString());

output输出

["art","0.0"," comedy","0.0"," action","0.0"," crime","0.0"," animals","0.0"]

For such a trivial example, it's pretty easy to produce a poorly-formed JSON String of your content and let JSONObject patch it up.对于这样一个简单的例子,很容易为您的内容生成一个格式不正确的 JSON String并让JSONObject修补它。

In a single expression:在单个表达式中:

new JSONObject(String.format("{%s}", str.replaceAll("([^,]+),([^,]+)(,|$)", "$1:$2,")))
// {"art":0,"comedy":0,"action":0,"crime":0,"animals":0}

If you really want to keep the 0.0 as String s:如果您真的想将0.0保留为String s:

new JSONObject(String.format("{%s}", str.replaceAll("([^,]+),([^,]+)(,|$)", "$1:\"$2\",")))
// {"art":"0.0","comedy":"0.0","action":"0.0","crime":"0.0","animals":"0.0"}

If you want to account for possible extraneous whitespaces:如果要考虑可能的无关空格:

new JSONObject(String.format("{%s}", str.replaceAll("([^,]+)\\s*?,\\s*?([^,]+)(,|$)", "$1:$2,")))

.. will work with inputs like "art, 0.0, comedy, 0.0, action, 0.0, crime, 0.0, animals, 0.0" and other cases. .. 将处理"art, 0.0, comedy, 0.0, action, 0.0, crime, 0.0, animals, 0.0"和其他情况。


Disclaimer: it's not crazy-sexy code but drop in a one-line comment and it could be reasonable so long as the data structure stays simplistic.免责声明:它不是疯狂性感的代码,而是加入一行注释,只要数据结构保持简单,它就可能是合理的。

You should use Map instead of Array (List of key/value pair is Map, not Array).您应该使用 Map 而不是 Array (键/值对列表是 Map,而不是 Array)。

1 way: Using JsonObject 1 种方式:使用 JsonObject

String [] arrayStr=strVal.split(",");
JsonObjectBuilder builder = Json.createObjectBuilder()

String key = null;
for (String s: arrayStr){
    if(key == null) {
       key = s;
    } else {
       builder.add(key, s);
       key = null;
    }
}
JsonObject value = builder.build();

2 way: Using Map 2方式:使用地图

String [] arrayStr=strVal.split(",");
Map<String,String> map = new HashMap<>();

String key = null;
for (String s: arrayStr){
    if(key == null) {
       key = s;
    } else {
       map.put(key, s);
       key = null;
    }
}

// convert Map to Json using any Json lib (Gson, Jackson and so on)

It looks like you want to use JSONObject not JSONArray to get the desired output.看起来您想使用 JSONObject 而不是 JSONArray 来获得所需的输出。 ie just add the items to the object - jsonObject.put(key, value)即只需将项目添加到对象 - jsonObject.put(key, value)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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