導航:首頁 > 編程語言 > javajson獲取key

javajson獲取key

發布時間:2023-04-16 14:42:07

1. java JsonObject怎麼判斷一個json串中是否含有某個key值

代碼:

json.get("key")

(括弧里的是你要判斷的值或者參數)

2. java中從josn中取出相同key所對應的另一個key的集合

publicstaticvoidmain(String[]args)
{
Stringstring="[{"depid":"5","score":"10"},{"depid":"4","score":"40"},{"depid":"4","score":"30"},{"depid":"5","score":"30"}]";
JSONArrayfromObject=JSONArray.fromObject(string);
Map<String,Integer>map=newHashMap<String,Integer>();

for(Objectobject:fromObject)
{
轎激JSONObjectjsonObject=(JSONObject)object;
Stringdepid=(String)jsonObject.get("depid");
Integerscore=Integer.valueOf((String)jsonObject.get("score"));
梁帆賀if(map.containsKey(depid))
{
intinteger=map.get(depid);
map.put(depid,integer+score);
}
else
{
map.put(depid,score);
橡派}
}
Set<Entry<String,Integer>>entrySet=map.entrySet();
JSONArrayjsonArray=newJSONArray();

for(Entry<String,Integer>entry:entrySet)
{
JSONObjectjsonObject=newJSONObject();
jsonObject.put("depid",entry.getKey());
jsonObject.put("score",String.valueOf(entry.getValue()));
jsonArray.add(jsonObject);
}
System.out.println(jsonArray.toString());
}

3. Java如何獲取Json相同key對應的value

可以將這些字元串放在Java的JsonObject類中,通過這個唯談類的get方法獲卜山鏈取key相對應的value的值型孫

4. Java解析json數據

一、 JSON (JavaScript Object Notation)一種簡單的數據格式,比xml更輕巧。
Json建構於兩種結構:
1、「名稱/值」對的集合(A collection of name/value pairs)。不同的語言中,它被理解為對象(object),紀錄(record),結構(struct),字典(dictionary),哈希表(hash table),有鍵列表(keyed list),或者關聯數組 (associative array)。 如:
{
「name」:」jackson」,
「age」:100
}

2、值的有序列表(An ordered list of values)。在大部分語言中,它被理解為數組(array)如:
{
「students」:
[
{「name」:」jackson」,「age」:100},
{「name」:」michael」,」age」:51}
]
}
二、java解析JSON步驟
A、伺服器端將數據轉換成json字元串
首先、伺服器端項目要導入json的jar包和json所依賴的jar包至builtPath路徑下(這些可以到JSON-lib官網下載:http://json-lib.sourceforge.net/)

然後將數據轉為json字元串,核心函數是:
public static String createJsonString(String key, Object value)
{
JSONObject jsonObject = new JSONObject();
jsonObject.put(key, value);
return jsonObject.toString();
}
B、客戶端將json字元串轉換為相應的javaBean
1、客戶端獲取json字元串(因為android項目中已經集成了json的jar包所以這里無需導入)
public class HttpUtil
{

public static String getJsonContent(String urlStr)
{
try
{// 獲取HttpURLConnection連接對象
URL url = new URL(urlStr);
HttpURLConnection httpConn = (HttpURLConnection) url
.openConnection();
// 設置連接屬性
httpConn.setConnectTimeout(3000);
httpConn.setDoInput(true);
httpConn.setRequestMethod("GET");
// 獲取相應碼
int respCode = httpConn.getResponseCode();
if (respCode == 200)
{
return ConvertStream2Json(httpConn.getInputStream());
}
}
catch (MalformedURLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}

private static String ConvertStream2Json(InputStream inputStream)
{
String jsonStr = "";
// ByteArrayOutputStream相當於內存輸出流
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
// 將輸入流轉移到內存輸出流中
try
{
while ((len = inputStream.read(buffer, 0, buffer.length)) != -1)
{
out.write(buffer, 0, len);
}
// 將內存流轉換為字元串
jsonStr = new String(out.toByteArray());
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return jsonStr;
}
}
2、獲取javaBean
public static Person getPerson(String jsonStr)
{
Person person = new Person();
try
{// 將json字元串轉換為json對象
JSONObject jsonObj = new JSONObject(jsonStr);
// 得到指定json key對象的value對象
JSONObject personObj = jsonObj.getJSONObject("person");
// 獲取之對象的所有屬性
person.setId(personObj.getInt("id"));
person.setName(personObj.getString("name"));
person.setAddress(personObj.getString("address"));
}
catch (JSONException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}

return person;
}

public static List<Person> getPersons(String jsonStr)
{
List<Person> list = new ArrayList<Person>();

JSONObject jsonObj;
try
{// 將json字元串轉換為json對象
jsonObj = new JSONObject(jsonStr);
// 得到指定json key對象的value對象
JSONArray personList = jsonObj.getJSONArray("persons");
// 遍歷jsonArray
for (int i = 0; i < personList.length(); i++)
{
// 獲取每一個json對象
JSONObject jsonItem = personList.getJSONObject(i);
// 獲取每一個json對象的值
Person person = new Person();
person.setId(jsonItem.getInt("id"));
person.setName(jsonItem.getString("name"));
person.setAddress(jsonItem.getString("address"));
list.add(person);
}
}
catch (JSONException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}

return list;
}

5. JAVA 中 提取 alibaba.fastjson.JSONObject 字元串中的 KEY 和 VALUE 值

java.util.Iterator it = hashmap.entrySet().iterator();

while(it.hasNext()){

java.util.Map.Entry entry = (java.util.Map.Entry)it.next();

entry.getKey() 返回與此項對應蠢談胡侍陪的鍵

entry.getValue() 返帶攔回與此項對應的值

}

6. java後台怎樣獲取前台的json數據

一般是這樣的,先缺枝將對象類型(在js中json就是一個對象),使用json.js(有些瀏覽器支持,有些需要引入)的方法:

JSON.parse(jsonstr); //可伏哪敏以將json字元串緩余轉換成json對象
JSON.stringify(jsonobj); //可以將json對象轉換成json對符串
的第二個轉換成字元串,然後把字元串當作一個參數的值再為其添加一個key然後以key/value的格式將數據傳到後台。

之後後台使用request.getParameter();的方式得到json數據。

7. json中怎麼獲取key

假如你的json是這樣辯侍的格式拍仿:var json = {『key_1』:123, 'key_2':234, 'key_3':567};
那麼你可以用循環的方襲灶纖式拿到:
for(var i in json){
alert("key="+i)
}

如果是用java處理json的話,那麼先轉換成JSONObject對象:
String string = "{key_1:123, key_2:234, key_3:567}";
JSONObject json= JSONObject.fromObject(string);
Set<String> set = json.keySet();
for(String key : set){
System.out.println("key=="+key);
}

閱讀全文

與javajson獲取key相關的資料

熱點內容
手機app上如何參加醫保 瀏覽:823
小米手錶怎麼復制加密卡 瀏覽:699
雲伺服器跑腳本掙錢 瀏覽:746
跳舞解壓釋放 瀏覽:351
內存測試軟體檢測加密 瀏覽:913
工作表頭文件加密怎麼設置 瀏覽:981
python獲取字元串編碼 瀏覽:102
java獲取當前系統時間 瀏覽:369
武漢有python培訓嗎 瀏覽:658
為什麼無法與伺服器建立數據鏈接 瀏覽:190
友價源碼2017 瀏覽:596
體溫偵測系統python 瀏覽:118
為什麼安卓系統佔用百分比 瀏覽:419
浪潮雲伺服器的組成部分 瀏覽:409
php100教程目錄 瀏覽:580
查看文件夾大小的命令 瀏覽:664
unixset命令 瀏覽:194
東北證券融e通app有什麼用 瀏覽:515
科大訊飛linux 瀏覽:466
三浪三副圖指標源碼 瀏覽:57