How to convert a string that contains HTML encoding characters to NSData? - html

I need to convert this string
"{
"name":"Tsubasa En La Liga",
"description":"T\u1ebfp sau b\u1ed9 tsubasa El classico\u2026anh \u0111\u00e3 tr\u1edf l\u1ea1i, g\u1eb7p l\u1ea1i nh\u1eefng b\u1ea1n b\u00e8 v\u00e0 \u0111\u1ed1i th\u1ee7 c\u0169 v\u00e0 c\u1ed1 g\u1eafng \u0111\u01b0a \u0111\u1ed9i tuy\u1ec3n nh\u1eadt \u0111o\u1ea1t chi\u1ebfc c\u00fap v\u00f4 \u0111\u1ecbch"
}"
to a NSData object.
I used
NSData* data = [NSData dataWithBytes:[string cStringUsingEncoding:NSASCIIStringEncoding] length:[string lengthOfBytesUsingEncoding:NSASCIIStringEncoding]];
or
NSData* data =[data dataUsingEncoding:NSASCIIStringEncoding];
And then save data to a Core Data entity.
But when I read data from the entities the result is utf8 encoded.
"{
"name":"Tsubasa En La Liga",
"description":"Tếp sau bộ tsubasa El classico…anh đã trở lại, gặp lại những bạn bè và đối thủ cũ và cố gắng đưa đội tuyển nhật đoạt chiếc cúp vô địch"
}"
I don't want the string encoded to utf8.
I want the original string should be save to Core Data entity.
Thank you.

you can fix that by escaping your backslashes. use double back-slash \\.

Related

Failure in Deserialization of File (NewtonSoft.JSON & VB.NET) [duplicate]

This question already has answers here:
How can I parse a JSON string that would cause illegal C# identifiers?
(3 answers)
Cannot deserialize the JSON array (e.g. [1,2,3]) into type ' ' because type requires JSON object (e.g. {"name":"value"}) to deserialize correctly
(6 answers)
Closed 1 year ago.
I have a small test file I'm trying to work with to deserialize the data into a model. I've already this using tokens in NewtonSoft.JSON but would rather do this properly with a model.
Example JSON
[
{
"faction": "Britain",
"id": "aa_barrage",
"image": "aa_barrage.png",
"import_id": "02",
"kredits": 1,
"rarity": "Standard",
"set": "Base",
"text": {
"de-DE": "Die gewählte Lufteinheit muss sich zurückziehen. Gibt deinem HQ +2 Verteidigung.",
"en-EN": "Target air unit must retreat. Give your HQ +2 defense.",
"es-ES": "La unidad aérea seleccionada debe retirarse. Otorga un +2 de defensa a tu cuartel general.",
"fr-FR": "L'unité aérienne visée doit battre en retraite. Confère +2 de défense à votre QG",
"it-IT": "L'unità aerea bersaglio deve Ritirarsi. Dai al tuo QG +2 difesa.",
"pl-PL": "Wybrana jednostka powietrzna musi się wycofać. Zapewnij swojemu sztabowi +2 do obrony.",
"pt-BR": "A unidade aérea na mira deve recuar. Dê +2 de defesa ao seu QG.",
"ru-RU": "Выбранный воздушный отряд должен отступить. Ваш штаб получает +2 к защите.",
"zh-Hans": "使一个空军撤退,你的总部获得 +2 防御力。",
"zh-Hant": "使一個空軍撤退,你的總部獲得 +2 防禦力。"
},
"title": {
"de-DE": "FLAK-SPERRFEUER",
"en-EN": "AA BARRAGE",
"es-ES": "BOMBARDEO AA",
"fr-FR": "BARRAGE AA",
"it-IT": "CONTRAEREA",
"pl-PL": "ZAPORA PRZECIWLOTNICZA",
"pt-BR": "BARRAGEM AA",
"ru-RU": "ЗЕНИТНЫЙ ОГОНЬ",
"zh-Hans": "防空弹幕",
"zh-Hant": "防空彈幕"
},
"type": "order"
},
{
"faction": "Britain",
"id": "active_sonar",
"image": "active_sonar.png",
"import_id": "fy",
"kredits": 0,
"rarity": "Elite",
"set": "OnlySpawnable",
"text": {
"de-DE": "Setzt alle feindlichen Einheiten fest. Karten in der gegnerischen Hand kosten 3 mehr.",
"en-EN": "Pin all enemy units. Increase cost of all cards in enemy hand by 3.",
"es-ES": "Inmoviliza todas las unidades enemigas. Las cartas en la mano del enemigo cuestan 3 más.",
"fr-FR": "Immobilise toutes les unités ennemies. Les cartes dans la main de l'ennemi coûtent 3 de plus.",
"it-IT": "Blocca tutte le unità nemiche. Aumenta di 3 il costo delle carte nella mano del nemico.",
"pl-PL": "Przyszpil wszystkie wrogie jednostki. Koszt kart w ręce przeciwnika wzrasta o 3.",
"pt-BR": "Imobilize todas as unidades inimigas. Todas as cartas na mão do inimigo custam mais 3.",
"ru-RU": "Блокирует все вражеские отряды. Карты в руке противника стоят на 3 больше.",
"zh-Hans": "压制敌方所有单位。对手所有手牌花费 +3。",
"zh-Hant": "壓制敵方所有單位。對手所有手牌花費 +3。"
},
"title": {
"de-DE": "AKTIVES SONAR",
"en-EN": "ACTIVE SONAR",
"es-ES": "SONAR ACTIVO",
"fr-FR": "SONAR ACTIF",
"it-IT": "SONAR ATTIVO",
"pl-PL": "AKTYWNY SONAR",
"pt-BR": "SONAR ATIVO",
"ru-RU": "АКТИВНЫЙ СОНАР",
"zh-Hans": "主动声呐",
"zh-Hant": "主動聲納"
},
"type": "order"
}
]
The current code I have for this is the following:
Module Program
Sub Main(args As String())
dim json As string = File.ReadAllText("C:\Temp\cards.json")
dim result As cards = JsonConvert.DeserializeObject(Of cards)(json)
End Sub
End Module
Public Class cards
public property cards as list(of card)
End Class
Public Class card
public property faction as string
public property id as string
public property image as string
public property import_id as string
public property kredits as integer
public property rarity as string
<JsonProperty("set")>
public property cardset as string
public property type as string
End Class
I can deserialize just one of these on its own, however when I try and deserialize with both it fails due to the following:
Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'json_deserialize_test.cards' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
I have tried changing my deserialization to:
dim result As cards = JsonConvert.DeserializeObject(Of List(Of cards))(json)
but to no available, something that was mentioned in another question, can someone point me in the right direction here please, I feel like I've been looking at it too long and need another pair of eyes.

Remove '[ from JSON when writing to file in Python

I try to write a JSON to a file to use in another program. I have a dataframe which I make into a JSON like this:
jsontest = df_qnapairs.to_json(orient = 'records', force_ascii = False)
The output is this
Out[9]: '[{"id":1,"answer":"Status på Anleggsoverenskomsten er Oppgjør vedtatt","source":"Automatic","questions":["Hva er status på Anleggsoverenskomsten","Hvordan går det med Anleggsoverenskomsten","Når skjer Anleggsoverenskomsten","Status på Anleggsoverenskomsten","Fortell meg når Anleggsoverenskomsten skjer"],"metadata":[]},{"id":2,"answer":"Status på Kraftlinjefirmaer er Ikke oppgitt","source":"Automatic","questions":["Hva er status på Kraftlinjefirmaer","Hvordan går det med Kraftlinjefirmaer","Når skjer Kraftlinjefirmaer","Status på Kraftlinjefirmaer","Fortell meg når Kraftlinjefirmaer skjer"],"metadata":[]}]'
The problem is that the input program runs an error because of the preceding "'[" and trailing "']". How can I remove these when writing to the file?
if u remove "[" and "]" from it, you change its type from json to dict and in the other program you must be work with dict, i recommend to read and write on file in json format.
if u want to load json in other program you can use :
import json
json_var = json.loads(var)
if u force to do it, u can change it to string and remove "[" and "]" :
var = str(jsontest)[1:len(str(jsontest))-1]

I have a JSON file with 379000 lines and it is full with double quotes inside the string objects,how can I delete all of them?

The invalid format:
"studwhere": "Academia de Muzica "Gheorghe Dima" Cluj-Napoca",
Expected result:
"studwhere": "Academia de Muzica Gheorghe Dima Cluj-Napoca",
or
"studwhere": "Academia de Muzica \"Gheorghe Dima\" Cluj-Napoca",

cakephp 2.6 confirm message spanish chars fails due to json_encode

my view code:
$confirm='¿Está seguro de que quiere borrar el nº 1?';
$this->Html->link(__d('phkapa', 'Send'), array('action' => 'send', $ticket['Ticket']['id']),array('escape' => true, 'confirm'=> $confirm));
result in alert box is :
\u00bfEst\u00e1 seguro de que quiere borrar el n\u00ba 1?
cakephp 2.6 and all app is on utf8 ...
is this a bug on cake 2.6 or am i missing something? or any ideas how to resolve this without changing the core function that returns a string to be used as onclick handler for confirm dialogs.
this function uses json_encode like $message = json_encode($message) and this is the problem, the json_enconde outputs \u00bf instead of ¿
unfortunaly we cant use option JSON_UNESCAPED_UNICODE, that encodes multibyte Unicode characters literally (default is to escape as \uXXXX), since is only available since PHP 5.4.0.
Try encoding the value with PHP first via urlencode. Hopefully CakePHP decodes the values when it produces the HTML output.
$confirm = urlencode ( '¿Está seguro de que quiere borrar el nº 1?' );
$this->Html->link(__d('phkapa', 'Send'), array('action' => 'send', $ticket['Ticket']['id']),array('escape' => true, 'confirm'=> $confirm));
You can also see this JS fiddle https://jsfiddle.net/daeyebpu/ that demonstrates encoding/decoding.
Saludos desde Colorado

Strsplit error parsing JSON using RJSON

I've got a well formated JSON string with a R script, but when I try to parse it using the rjson package (with parser$addData), I get this error :
>json <- fromJSON(docret)
>parser <- newJSONParser()
>parser$addData(json)
Erreur dans strsplit(buf, "") :
l'argument n'est pas une chaîne de caractères
My JSON string (docret here) is validated by two online JSON validators.
Can I do something about this error to have my JSON object usable?
Here is my JSON :
http://shrib.com/8dTgq90U