2016-09-06 22 views
4

文字列をJSON形式(JSON形式である必要はありませんが、JSON形式と思われます)として読み込み、hashMap(key:Keyword、value :COUNT)JAVAでJSON形式の文字列を読み取る

たとえば、文字列があるとします。

String s ={"Welcome":1,"Hi":2,"Hello":1,"Jin":1}; 

次に、分類する(ハッシュマップキー - >単語、値 - >番号)。最終結果は以下のようになります。

HashMap<String,String> result; 

result.get("Jin"); // output : 1 
result.get("Hi"); // output : 2 

しかし、私のコードは正しい方法ではありません。

JSONParser parser = new JSONParser(); 
     Object obj = parser.parse(s); 
     JSONArray array = new JSONArray(); 
     array.add(obj); 

     System.out.println(array.get(0)); //output: {"Welcome":1,"Hi":2,"Hello":1,"Jin":1} 

JSONで可能ですか?またはそれらを1つずつ分割する必要がありますか? (例えば、 "、"、 ":"などで区切るなど)

私にあなたの親切なアドバイスをしてください。

+0

に見てください、それをupvoteと答えを受け入れてください。 –

答えて

1

以下のコードスニペットを試してください。

public static void main(final String[] args) throws ParseException { 
     String s = "{\"Welcome\":1,\"Hi\":2,\"Hello\":1,\"Jin\":1}"; 
     JSONParser parser = new JSONParser(); 
     HashMap<String, Long> obj = (HashMap<String, Long>) parser.parse(s); 
     for(String key : obj.keySet()) { 
      System.out.println("Key:" + key + " value:" + obj.get(key)); 
     } 
    } 
0

そのjson objectないarray ...

は、このいずれかを試してみてください。

JSONObject jsonObj = new JSONObject(jsonString.toString()); 
0

これを達成するのが最も簡単にはJACKSONのパーサを使用することです。

import java.util.HashMap; 
import java.util.Map; 

import org.codehaus.jackson.map.ObjectMapper; 
import org.codehaus.jackson.type.TypeReference; 

String s = "{\"Welcome\":1,\"Hi\":2,\"Hello\":1,\"Jin\":1}"; 
ObjectMapper mapper = new ObjectMapper(); 


Map<String, String> map = mapper.readValue(s, new TypeReference<HashMap<String, String>>() { 
}); 

map.forEach((k, v) -> System.out.println("Key is " + k + " value is " + v)); 

プリント:

Key is Hi value is 2 
Key is Hello value is 1 
Key is Welcome value is 1 
Key is Jin value is 1 
1

あなたの要件を満たすためにorg.jsonを使用することができます。

など。

String s = "{\"Welcome\":1,\"Hi\":2,\"Hello\":1,\"Jin\":1}"; 
JSONObject result = new JSONObject(s); 

System.out.println(result.get("Jin")); // output : 1 
System.out.println(result.get("Hi")); // output : 2 
0

使用GoogleのJSONすなわちgsonライブラリ(2.6.2)とあなたの問題が解決されます。以下の答えのいずれかの問題を解決するためにあなたを助けた場合

次のコード

import java.util.HashMap; 
import java.util.Map.Entry; 
import java.util.Set; 

import com.google.gson.Gson; 
import com.google.gson.JsonElement; 
import com.google.gson.JsonObject; 

public class StackOverFlowQuestionset { 

    public static void main(String[] args) { 
     String s ="{\"Welcome\":1,\"Hi\":2,\"Hello\":1,\"Jin\":1}"; 

     HashMap<String,String> result=new HashMap<String,String>(); 

     Gson gson = new Gson(); 

     JsonElement jsonElement = gson.fromJson(s, JsonElement.class); 

     JsonObject jsonObject = jsonElement.getAsJsonObject(); 

     Set<Entry<String, JsonElement>> jsonEntrySet = jsonObject.entrySet(); 

     for(Entry<String, JsonElement> entry:jsonEntrySet){ 
      result.put(entry.getKey(), entry.getValue().toString()); 
     } 

     System.out.println(result.get("Jin")); 
     System.out.println(result.get("Welcome")); 
     System.out.println(result.get("Hi")); 
    } 
} 
関連する問題