2017-10-16 195 views
0

這是我的問題。我從互聯網上得到一個隨機的json文件,如this onethis one在不知道JSON格式的情況下解析JSON(與selectToken一樣)

我想解析它與JSON在android studio(使用例如,gson)。但是我無法在gson中找到這樣的選項,讓我從JSON文件中選擇一個不知道JSON結構的標記(並創建一個類和那些東西)。當我試圖做到這一點在VisualBasic.NET它是很容易,使用此代碼和NewtonSoft.Json庫:

Dim jsonSet As JObject = JObject.Parse(responseFromServer) 
    balance = jsonSet.SelectToken("$..balance") 

但似乎更難的方式做到這一點在Java中...有人可以幫助我嗎?

+0

可能重複[如何使用Gson將JSON轉換爲HashMap?](https://stackoverflow.com/questions/2779251/how-can-i-convert-json-to-a-hashmap-using- gson) – logcat

回答

0

好了,終於做一些更多的研究後,我發現這一點: https://github.com/json-path/JsonPath

這正是我需要的,因爲它可以讓你訪問一個JSON對象,並找到其路徑的道理,正是因爲我NewtonSoft做.NET中的Json。

我認爲這真的很有趣,因爲不管JSON文件的結構如何,只要給出格式爲「$ .. path」的路徑即可找到值。

0

Gson是一個對象序列化/反序列化庫。其目的是序列化已知對象和已知對象。

你想使用一個更基本的庫,其中有幾個可用的實現。其中一些上市http://www.json.org/

它們允許你寫代碼像

JSONObject obj = new JSONObject("{}"); 
+0

是的,問題是我沒有找到任何庫來做我要求的選項。 Gson只是一個例子。你知道任何像NewtonSoft.Json一樣工作的圖書館嗎? – Xamortex

+0

https://mvnrepository.com/artifact/org.json/json – Deadron

0

您可以將您的JSON字符串粘貼到http://www.jsonschema2pojo.org/

它會創建jsonString的對象。

實例與您的JSON:

-----------------------------------com.example.Datum.java----------------------------------- 

package com.example; 

import com.google.gson.annotations.Expose; 
import com.google.gson.annotations.SerializedName; 

public class Datum { 

@SerializedName("address") 
@Expose 
private String address; 
@SerializedName("balance") 
@Expose 
private Integer balance; 
@SerializedName("nonce") 
@Expose 
private Object nonce; 
@SerializedName("code") 
@Expose 
private String code; 
@SerializedName("name") 
@Expose 
private Object name; 
@SerializedName("storage") 
@Expose 
private Object storage; 
@SerializedName("firstSeen") 
@Expose 
private String firstSeen; 

public String getAddress() { 
return address; 
} 

public void setAddress(String address) { 
this.address = address; 
} 

public Integer getBalance() { 
return balance; 
} 

public void setBalance(Integer balance) { 
this.balance = balance; 
} 

public Object getNonce() { 
return nonce; 
} 

public void setNonce(Object nonce) { 
this.nonce = nonce; 
} 

public String getCode() { 
return code; 
} 

public void setCode(String code) { 
this.code = code; 
} 

public Object getName() { 
return name; 
} 

public void setName(Object name) { 
this.name = name; 
} 

public Object getStorage() { 
return storage; 
} 

public void setStorage(Object storage) { 
this.storage = storage; 
} 

public String getFirstSeen() { 
return firstSeen; 
} 

public void setFirstSeen(String firstSeen) { 
this.firstSeen = firstSeen; 
} 

} 
-----------------------------------com.example.Example.java----------------------------------- 

package com.example; 

import java.util.List; 
import com.google.gson.annotations.Expose; 
import com.google.gson.annotations.SerializedName; 

public class Example { 

@SerializedName("status") 
@Expose 
private Integer status; 
@SerializedName("data") 
@Expose 
private List<Datum> data = null; 

public Integer getStatus() { 
return status; 
} 

public void setStatus(Integer status) { 
this.status = status; 
} 

public List<Datum> getData() { 
return data; 
} 

public void setData(List<Datum> data) { 
this.data = data; 
} 

} 

後,你會得到對象從jsonString與功能:

// Deserialize to single object. 
    public Example deserializeFromJson(String jsonString) { 
     Gson gson = new Gson(); 
     Example myClass = gson.fromJson(jsonString, Example.class); 
     return myClass; 
    } 

,你可以得到你的對象的一切。

我希望它能幫助你的問題!

+0

有了這個我仍然會遇到同樣的問題。我需要一個適用於多個非泛型json api的代碼。你的建議是爲每個JSON文件創建一個類,但我要求更多的類似:給定一個JSON文件,無論文件的結構如何,都可以獲取令牌的值,即使令牌在一個數組中。用我使用.NET粘貼NewtonSoft.Json的代碼,它工作簡單而且簡單。非常感謝您的幫助! – Xamortex