簡體   English   中英

將 JSON 字符串轉換為 Java/Python (Jython) object?

[英]Convert a JSON string to a Java/Python (Jython) object?

So I understand that you can convert JSON strings to strings and handle JSON objects in general through the org.json bundle in Android, but here's my current situation:

我需要從某個 URL 中取出一個 JSON 字符串(我已經能夠成功地做到這一點)並將其放入一個數組中。 好吧實際上是兩個 arrays。 我正在使用的框架在 Python 上運行並返回一個包含列表(Python 中的數組)的字典。 但是,它顯示為 JSON object。 這是我將從 URL 到我的 Java 代碼中得到的示例:

{"keywords": ["middle east", "syria"], "link": [["middle east", "http://www.google.com/#q=middle east"], ["syria", "http://www.google.com/#q=syria"]]}

如您所見,它是兩個索引的字典。 第一個是包含列表的“關鍵字”,第二個是包含列表列表的“鏈接”。 這兩個列表(第一個和第二個多維列表)是我希望能夠在 Java 中操作的。 我知道您可以使用 JSONArray,但問題是 arrays 存儲在 Python 字典中,而我的 Android 應用程序無法正確生成 JSONArray. 你們對我如何處理這個有任何想法嗎? 我很迷茫。 這是我獲取實際 JSON 字符串的代碼(代碼中的 URL 並非所有人都可以訪問,它是通過粘貼在我的機器上提供的):

static public void refreshFeed(){
    try{
        String url = "http://192.17.178.116:8080/getkw?nextline="+line;
        line++;
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet(url);
        HttpResponse response;
        response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        InputStream in = entity.getContent();

        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        StringBuilder sb = new StringBuilder();
            
        String input = null;
        try {
            while ((input = reader.readLine()) != null) {
            sb.append(input + "\n");
            }
        } catch (IOException e) {
                e.printStackTrace();
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
         }
        String enter = sb.toString();
        feedEntry add = new feedEntry(enter);
        addNewEntry(add);
        in.close();
        
    } catch(MalformedURLException e){
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

另請注意,這沒有將 JSONString 制成 JSONArray。 它只是將 JSON object 轉換為添加到“feedEntry” object 的常規字符串。

將 python 字典映射到 json 數組是……比您預期的要多。 最好將其放入 json object 或從列表開始,該列表可以直接映射到 json 數組。 有關 python 和 java 之間序列化的信息

這是一個代碼示例,我在 Python 中創建了一個列表結構,然后在 Android 應用程序中抓取它:

#!/usr/bin/python

print "Content-type: text/html\n\n"

import json
from collections import defaultdict

mystuff = list()
mystuff.append( ('1', 'b', 'c', 'd') )
mystuff.append( ('2', 'f', 'g', 'h') )

stufflist = list()

for s in stufflist:
    d = {}
    d['a'] = s[0]
    d['b'] = s[1]
    d['c'] = s[2]
    d['d'] = s[3]
    stufflist.append(d)

print json.write(stufflist)

在 Android 中:

// Convert the string (sb is a string butter from the http response) to a json array. 
JSONArray jArray = new JSONArray(sb.toString());

for(int i = 0; i < jArray.length(); i++){
    // Get each item as a JSON object. 
    JSONObject json_data = jArray.getJSONObject(i);

    // Get data from object ... 
    Int a = json_data.getInt("a");
    String b = json_data.getString("b");
    String c = json_data.getString("c");
    String d = json_data.getString("d");

    // Do whatever with the data ... 
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM