安卓:对JSON解析HTML标签并启用特殊字符 [英] Android: Parsing html tags on JSON and enabling special characters

查看:174
本文介绍了安卓:对JSON解析HTML标签并启用特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮助我,如何在JSON解析HTML标签?因为我正在做一个德国应用程序,它包含了大量的特殊字符,如ä,ö,ü,ß。有人能告诉我如何展示通过JSON解析这些字符?右键他们只显示为?

下面是我的JSON解析方法:

 无效examineJSONFile()
    {
        尝试
        {
            字符串x =;
            InputStream为= this.getResources()openRawResource(R.raw.json)。
            字节[]缓冲区=新的字节[is.available()];
            而(is.read(缓冲液)= -1!);
            字符串jsontext =新的String(缓冲);
            JSONArray项=新JSONArray(jsontext);            X =JSON解析\\ n有[+ entries.length()+] \\ n \\ n;            INT I;
            对于(i = 0; I< entries.length();我++)
            {
                JSONObject的岗位= entries.getJSONObject(I)
                X + = post.getString(影片名称)+\\ n;
            }
            titel.setText(X);        }
        赶上(例外JE)
        {
            txt_beschreibung.setText(错误重量/文件:+ je.getMessage());
        }    }

修改
这是我的JSON文件包含HTML标签的例子

  {
  影片名称:CARE德国,卢森堡E​​.V.
  关键词:照顾,
  beschreibung:< P>< B>达斯CARE-KOMPLETT-PAKET献给Menschen在不与LT; / B>< / P>< P>施奈尔,nachhaltig UND durchdacht,达斯北京时间DAS摩登CARE-一揽子贷款。 CARE ISTüberzeugt,DASS umfassende Hilfe冯DREI页数notwendig IST的那个死weltweite Armut Schritt献给Schritt祖verringern。Deswegen帽子CARE SICH seit围网渔船Gründung1945年UND马克Abwurf德ersten CARE-Pakete尤伯杯柏林weiter entwickelt。例如Heute steckt IM CARE-PAKET WEIT梅尔ALS朱克UND Mehl的例如Heute bietet死组织在70 DERärmsten州德世界报EIN KOMPLETT-PAKET献给Menschen在不与LT; / p>< p>< b>达斯KOMPLETT-PAKET献给Menschen在不enthält。: < / b>< / p> * sofortige Nothilfe NACH Katastrophen< BR>< BR> * langfristige Entwicklungszusammenarbeit< BR>< BR> *舒茨DER猩猩科动物< BR>< BR>中,
  bild_liste_url:http://cdn.spendino.de/web/img/projects/home/1284113658.jpg,
  bild_projekt_url:http://cdn.spendino.de/web/img/projects/small/1284113658.jpg,
  kosten:5
  }


解决方案

正在从资源(注意方法的名称 openRawResource )读取原始字节,所以你AREN 'T应用的字符编码​​。您需要使用的InputStreamReader (和的BufferedReader 通常是一个好主意):

  InputStream为= this.getResources()openRawResource(R.raw.json)。
作家作家=新的StringWriter();
的char []缓冲区=新的char [1024];尝试{
  读者的BufferedReader =新的BufferedReader(
    新的InputStreamReader(是,UTF-8)
  );
  INT N;
  而((N = reader.read(缓冲液))!= - 1){
    writer.write(缓冲液,0,N);
  }
} {最后
  is.close();
}串jsontext = writer.toString();

您可能需要使用不同的字符编码​​(的第二个参数的InputStreamReader )。

BTW,这已无关HTML标签。也许我失去了一些东西?

Can someone help me, how to parse HTML tags in JSON? And since I'm making a German App, it includes a lots of special characters like ä,ö,ü,ß. Can somebody tell me how to show those characters through parsing JSON? Right they are only shown as '?'

Here's my JSON parsing method:

void examineJSONFile()
    {
        try
        {
            String x = "";
            InputStream is = this.getResources().openRawResource(R.raw.json);
            byte [] buffer = new byte[is.available()];
            while (is.read(buffer) != -1);
            String jsontext = new String(buffer);
            JSONArray entries = new JSONArray(jsontext);

            x = "JSON parsed.\nThere are [" + entries.length() + "]\n\n";

            int i;
            for (i=0;i<entries.length();i++)
            {
                JSONObject post = entries.getJSONObject(i);
                x += post.getString("titel") + "\n";
            }
            titel.setText(x);

        }
        catch (Exception je)
        {
            txt_beschreibung.setText("Error w/file: " + je.getMessage());
        }

    }

EDIT Here's an example of my JSON file containing html tags

{
  "titel": "CARE Deutschland-Luxemburg e.V.",
  "keyword": "CARE",
  "beschreibung": "<p><b>Das CARE-Komplett-Paket für Menschen in Not</b></p><p>Schnell, nachhaltig und durchdacht, das ist das moderne CARE-Paket. CARE ist überzeugt, dass umfassende Hilfe von drei Seiten notwendig ist, um die weltweite Armut Schritt für Schritt zu verringern. Deswegen hat CARE sich seit seiner Gründung 1945 und dem Abwurf der ersten CARE-Pakete über Berlin weiter entwickelt. Heute steckt im CARE-Paket weit mehr als Zucker und Mehl. Heute bietet die Organisation in 70 der ärmsten Länder der Welt ein Komplett-Paket für Menschen in Not.</p><p><b>Das Komplett-Paket für Menschen in Not enthält:</b></p>*sofortige Nothilfe nach Katastrophen<br><br>*langfristige Entwicklungszusammenarbeit<br><br>*Schutz der Menschenrechte<br><br>",
  "bild_liste_url": "http://cdn.spendino.de/web/img/projects/home/1284113658.jpg",
  "bild_projekt_url":"http://cdn.spendino.de/web/img/projects/small/1284113658.jpg",
  "kosten": "5"
  }

解决方案

You are reading the raw bytes from the resource (notice the method name openRawResource), so you aren't applying a character encoding. You need to use an InputStreamReader (and a BufferedReader is usually a good idea):

InputStream is = this.getResources().openRawResource(R.raw.json);
Writer writer = new StringWriter();
char[] buffer = new char[1024];

try {
  BufferedReader reader = new BufferedReader(
    new InputStreamReader(is, "UTF-8")
  );
  int n;
  while ((n = reader.read(buffer)) != -1) {
    writer.write(buffer, 0, n);
  }
} finally {
  is.close();
}

String jsontext = writer.toString();

You may need to use a different character encoding (second parameter of InputStreamReader).

BTW, this has nothing to do with "HTML tags". Maybe I am missing something?

这篇关于安卓:对JSON解析HTML标签并启用特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆